C++程序模拟N个骰子的投掷


以下代码可模拟投掷N个骰子。可通过在1-6之间生成随机数来完成此操作。

算法

Begin
   Declare n
   Read n
   For i = 0 to n-1 do
      Generate sequence with rand() mod 6 + 1
      Print the sequence
   Done
End

示例代码

#include <iostream>
using namespace std;
int main(int argc, char **argv) {
   cout << "Enter the number of dice: ";
   int n;
   cin >> n;
   cout << "The values on dice are: ";
   for (int i = 0; i < n; i++)
      cout << (rand() % 6) + 1<<" ";
}

输出

Enter the number of dice: The values on dice are: 2 5 4 2 6 2 5

更新于:30-7-2019

988次浏览

开启你的事业

通过完成课程获得认证

开始吧
广告
© . All rights reserved.