rolldie.cpp

来自「C++上机课的习题答案」· C++ 代码 · 共 50 行

CPP
50
字号
// rolldie.cpp
//掷骰子,共两粒 
//求投掷36000次后,每个可能点数(2-12)出现的次数及其占投掷次数的百分比。 
#include <iostream>

using std::cout;
using std::endl;
using std::fixed;

#include <iomanip>

using std::setprecision;
using std::setw;

#include <cstdlib>
#include <ctime>

int main()
{
   const long ROLLS = 36000;
   const int SIZE = 13;

   /* Write declaration for array sum(数组名) here.*/
   
   /* Initialize all elements of array sum to zero. 
      Use SIZE for the number of elements */
   
   int die1;
   int die2;

   srand( time( 0 ) );
   
   /* Write a for loop(循环) that iterates(遍历) ROLLS times. Randomly
      generate values for x (i.e., die1) and y (i.e., die2)
      and increment the appropriate(适当的) counter in array sum that 
      corresponds to(相应) the sum of x and y */

   cout << setw( 10 ) << "Sum" << setw( 10 ) << "Total"
        << setw( 10 ) << "Actual\n" << fixed<< setprecision( 3 );

   for ( int j = 2; j < SIZE; ++j )
      cout << setw( 10 ) << j << setw( 10 ) << sum[ j ] 
           << setw( 9 ) << 100.0 * sum[ j ] / ROLLS << "%\n";
   
   system("PAUSE");
   return 0;

} // end main

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?