使用者工具

網站工具


cpp:homework:hw1answer

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

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;
}

cpp/homework/hw1answer.txt · 上一次變更: 2019/07/02 15:01 (外部編輯)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki