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.