dieroll2.cpp

来自「C++&datastructure书籍源码,以前外教提供现在与大家共享」· C++ 代码 · 共 35 行

CPP
35
字号
#include <iostream>using namespace std;#include "dice.h"#include "prompt.h"#include "tvector.h"// use vector to simulate rolling of two dice// Owen Astrachan, March 9, 1994, modified 5/2/99const int DICE_SIDES = 4;int main(){    int sum;    int k;    Dice d(DICE_SIDES);    tvector<int> diceStats(2*DICE_SIDES+1);  // room for largest dice sum    int rollCount = PromptRange("how many rolls",1,20000);    for(k=2; k <= 2*DICE_SIDES; k++)         // initialize counters to zero    {   diceStats[k] = 0;    }    for(k=0; k < rollCount; k++)             // simulate all the rolls    {   sum = d.Roll() + d.Roll();        diceStats[sum]++;    }    cout << "roll\t\t# of occurrences" << endl;    for(k=2; k <= 2*DICE_SIDES; k++)    {   cout << k << "\t\t" << diceStats[k] << endl;    }    return 0;}

⌨️ 快捷键说明

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