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