國立屏東大學 資訊工程系 物件導向程式設計

hw1-1 參考解答


using namespace std;
#include <iostream>
#include <cstdlib>
 
int points[8]={};
int count[6]={};
 
int main()
{
  int a, b, c;
  int mostAppeared=0;
 
  srand(time(NULL));
  for(int i=1;i<=100;i++)
  {
    if(i==1)
      cout << "Throwin dice ...\n1...";
    if(((i%10)==0)&&(i<100))
      cout << i << "...";
    if(i==100)
      cout << "100\n" << endl;
 
    a = (rand()%6)+1;
    b = (rand()%6)+1;
    c = (rand()%6)+1;
 
    count[a-1]++;
    count[b-1]++;
    count[c-1]++;
 
    if((a==b)&&(a==c))
    {
      if(a!=6)
        points[6]++;
      else
        points[7]++;
    }
    else
    {
      if(a==b)
        points[c-1]++;
      else if(a==c)
        points[b-1]++;
      else if(b==c)
        points[a-1]++;
    }
 
  }
  cout << "Statistical Data\nPoints" << endl;
  for(int i=0;i<6;i++)
    cout << i+1 << " : " << points[i] << endl;
  cout << "10 : " << points[6] << endl;
  cout << "20 : " << points[7] << endl << endl;
 
  for(int i=1;i<6;i++)
    if(count[i] > count[mostAppeared])
      mostAppeared = i;
 
  cout << "The most appeared dice-point is " << mostAppeared+1 << "." << endl;
 
  return 0;
}