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
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
安卓
Python
C程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP