模拟两个骰子的滚动并保存在矢量中
Simuate Rolling Of Two Dice and Save Sum In Vector
我正在尝试制作此代码,以滚动两个骰子并跟踪向量中的总和。我想出了以下代码,但是如果我输入以上的数字12。
#include "std_lib_facilities.h"
void simulate(vector<int>&, size_t);
void size_int(const vector<int>, size_t);
void print_vector(const vector<int>&, size_t);
int main()
{
size_t n;
cout << "Enter the amount of rolls: ";
cin >> n;
vector<int> v(12);
for(int i=0;i<v.size();i++)
{
v[i] = 0;
}
simulate(v, n);
print_vector(v, n);
}
void simulate(vector<int>& sum, size_t n)
{
int dice_1, dice_2;
cout << "Rolling the dice " << n << " times produces:n";
srand( time(NULL) );
size_t i;
for ( i = 0; i < n; ++i){
dice_1 = 1+(rand() % 6);
dice_2 = 1+(rand() % 6);
sum[dice_1+dice_2]++;
}
}
void print_vector(const vector<int>& v, size_t n)
{
size_t i;
double x = 1.0*n;
cout << "RolltFrequencytProbablityn";
for(i = 1;i<v.size();++i)
{
cout << i+1 << "t" << v[i] << "tt" << v[i]/x << endl;
}
}
sum[dice_1+dice_2]++;
dice1+dice2
将是2至12(包括12)的值。sum
仅具有元素0至11包含元素。您需要大小sum
至 13 才能做到这一点。
当您的向量从0开始您应该将值存储到
sum[dice_1+dice_2-1]++;
并在打印时。
从
开始for(i=0....)
相关文章:
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 使用一个考虑到std::map中键值的滚动或换行的键
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 在C++中将类(带有Vector成员)保存为二进制文件
- C++.我想将更改后的特定字符行保存在字符串中
- 使用FFMPEG将RGB图像序列保存到.mp4时出现问题
- 将RGB图像保存为PPM格式
- QScrollArea:由垂直滚动条引起的水平滚动条
- 跟踪滚动条上的鼠标事件
- 哈希文件递归并保存到矢量Cryptopp中
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 无法通过头文件保存变量的值
- 我们可以将数据永久保存为数据结构吗?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 保存/加载大量短数组到二进制文件
- 模拟两个骰子的滚动并保存在矢量中