- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题不太可能帮助任何 future 的访客;它仅与一个小地理区域、一个特定时刻或一个非常狭窄的情况相关,而这些情况通常不适用于互联网的全局受众。如需帮助使这个问题更广泛地适用,visit the help center .
8年前关闭。
这是我的任务:
在 Puzzlevania 的土地上,Aaron、Bob 和 Charlie 争论了他们中的哪一个
是有史以来最伟大的解谜者。为了一劳永逸地结束争论,他们同意
在生死决斗中。亚伦的射门很差,只有
1/3。 Bob 稍微好一点,以 1/2 的概率击中了他的目标。查理是前任
完美的神射手,从未错过。击中意味着杀死,被击中的人退出决斗。
为了弥补枪法上的不公平,三人决定
将轮流进行,从 Aaron 开始,然后是 Bob,然后是 Charlie。循环
会重复,直到有一个人站着。那个男人会被所有人铭记
时间是有史以来最伟大的解谜者。
一个明显而合理的策略是让每个人仍然向最准确的射手射击
活着,理由是这个射手是最致命的,并且有最好的反击机会。
编写一个程序来模拟使用这种策略的决斗。你的程序应该使用随机
问题中给出的数字和概率来确定射手是否击中他的
目标。您可能需要创建多个子例程和函数来完成
问题。
暗示
假设 Aaron 击中目标的几率是 1/3。您可以通过以下方式模拟此概率
生成一个介于 1 和 99 之间的随机变量 R。发生的概率是多少
R小于33?是 1/3。你知道这会导致什么吗?如果 R 33,那么我们可以
模拟 Aaron 击中目标。如果 R > 33,那么他错过了。也可以随机生成
0 到 1 之间的变量,并检查它是否小于 0.33。
这是我从头开始重写两次后终于能够编写的代码:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main ()
{
int aaron=1, bob=1,charlie=1, a=0,b=0,c=0,counter=0;
srand(time(0));
a= rand() % 100;
if (a<=33)//if aaron kills charlie
{
charlie=0;
b= rand() % 100; //bob choots at aaron
if (b<=50) //if bob kills aaron
{
aaron=0;
cout<<"error1"<<endl;
}
else if (b>50)
{
while (b>50) //if bob misses
{
a= rand() % 100; //aaron shoots at bob
if (a<=33)// if he kills bob
{
bob=0;
cout<<"error2"<<endl;
}
else //if he misses
{
b= rand() % 100;//bob shoots at aaron
if (b<=50)//if he kills him
{
aaron=0;
cout<<"error3"<<endl;
}
else //if he misses then the loop continues
{
counter++;
}
}
}
}
}
else if (a>33)//aaron misses
{
b= rand() % 100; //bob shoots at charlie
if (b<=50) //he kills charlie
{
charlie = 0;
a= rand() % 100; //aaron shoots at bob
if (a<=33)//aaron kills bob
{
bob=0;
cout<<"error4"<<endl;
}
else //if not
{
b= rand() % 100;//bob shoots at aaron
if (b<=50) // if he kills aaron
{
aaron=0;
cout<<"error5"<<endl;
}
else if (b>50)
{
while (b>50)//if not then begin loop
{
a= rand() % 100; //aaron shoots at bob
if (a<=33) //if he kills him
{
bob=0;
cout<<"error6"<<endl;
}
else
{
b= rand() % 100;; //if not bob shoots at aaron
if (b<=50)// if he kills aaron
{
aaron=0;
cout<<"error7"<<endl;
}
else
{
counter++; //if not loop around
}
}
}
}
}
}
else //he misses so charlies kills bob
{
bob=0;
a= rand() % 100; //aaron shoots at charlie
if (a<=33) //aaron kills charlie
{
charlie=0;
cout<<"error8"<<endl;
}
else // or charlie kills aaron
{
aaron=0;
cout<<"error9"<<endl;
}
}
if (charlie==0 && bob==0)
{
cout<<"Aaron wins."<<endl;
}
else if (aaron==0 && bob==0)
{
cout<<"Charlie wins."<<endl;
}
else if (charlie==0 && aaron==0)
{
cout<<"Bob wins."<<endl;
}
}
return 0;
}
最佳答案
第一点 - a= rand() % 100;
不给 1..99 包括在内,它给 0..99 包括在内。您可以使用 a = (rand() % 99) + 1
反而。这仍然不是一个完全均匀的分布,因为 rand
给出的随机数范围可能不能被 99 整除。可能的修复包括...
rand()
给出一个值 >= 9900
,例如,把它扔掉再试一次。将此循环放在一个函数中,这样您就不必每次都担心它。 if (b<=50) //if bob kills aaron
{
aaron=0;
cout<<"error1"<<endl;
}
else if (b>50)
if (b>50)
这里是完全多余的,至少对编译器来说是这样。考虑到您在这里有相当长的代码块并带有一些深层嵌套,您可能会为了清楚起见将其包括在内,但我会将其作为注释,并让
else
做它的工作。
while (b>50) //if bob misses
{
a= rand() % 100; //aaron shoots at bob
if (a<=33)// if he kills bob
{
bob=0;
cout<<"error2"<<endl;
}
else //if he misses
{
b= rand() % 100;//bob shoots at aaron
if (b<=50)//if he kills him
{
aaron=0;
cout<<"error3"<<endl;
}
else //if he misses then the loop continues
{
counter++;
}
}
}
b
未修改,因此循环再次重复。 Aaron 可以一遍又一遍地射击 Bob,直到他最终没有命中,即使 Bob 已经死了,他也终于得到了转身。你需要改变你的循环条件——可能在其他情况下也是如此,尽管我没有检查过。我可能会使用
done
旗帜。
a
不需要单独的变量。和
b
(而且我认为您甚至没有使用
c
) - 只需使用单个变量
r
对于
random
或类似的。
while num_alive > 1
if aaron_alive
if charlie_alive
aaron tries to shoot charlie
else
aaron tries to shoot bob
if bob_alive
if charlie_alive
bob tries to shoot charlie
else
bob tries to shoot aaron
if charlie_alive
if bob_alive
charlie tries to shoot bob
else
charlie tries to shoot aaron
if <whoever>_alive
的相似度。 block 是,以及如何将它们分解为函数。
关于c++ - 带有循环和消除的随机生成概率游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14994767/
我想使用 li 和 ul 制作一个多级下拉列表,以便显示我博客中按年和月排序的所有文章。我希望我的下拉菜单看起来像 Google Blogspot 下拉菜单: 这是我的 CSS 和 HTML 代码 u
我在 Win 7 64 机器上将 CodeBlocks 与 gcc 4.7.2 和 gmp 5.0.5 结合使用。开始使用 gmpxx 后,我看到一个奇怪的段错误,它不会出现在 +、- 等运算符中,但
我正在使用 tern 为使用 CodeMirror 运行的窗口提供一些增强的智能感知,它工作正常,但我遇到了一个问题,我想添加一些自定义“types”,可以这么说,这样下拉列表中它们旁边就有图标了。我
我正在尝试让我的 PC 成为 Android 2.3.4 设备的 USB 主机,以便能够在不需要实际“附件”的情况下开发 API。为此,我需要将 PC 设置为 USB 主机和“设备”(在我的例子中是运
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
我在设置服务器方面几乎是个新手,但遇到了一个问题。我有一个 Ubuntu 16.04 VPS 并安装了 Apache2 和 Tomcat7。我正在为 SSL 使用 LetsEncrypt 和 Cert
我在一个基于谷歌地图的项目上工作了超过 6 个月。我使用的是 Google Maps API V1 及其开发人员 API key 。当我尝试发布应用程序时,我了解到 Google API V1 已被弃
我是 Python 的新手,所以如果我对一些简单的事情感到困惑,请原谅。 我有一个这样的对象: class myObject(object): def __init__(self):
这个问题已经有答案了: How can I access object properties containing special characters? (2 个回答) 已关闭 9 年前。 我正在尝
我有下面的 CSS。我想要的是一种流体/液体(因为缺乏正确的术语)css。我正在为移动设备开发,当我改变模式时 从纵向 View 到陆地 View ,我希望它流畅。现在的图像 在陆地 View 中效
我正在尝试使用可以接受参数的缓存属性装饰器。 我查看了这个实现:http://www.daniweb.com/software-development/python/code/217241/a-cac
这个问题在这里已经有了答案: Understanding slicing (36 个答案) 关闭 6 年前。 以a = [1,2,3,4,5]为例。根据我的直觉,我认为 a[::-1] 与 a[0:
mysqldump -t -u root -p mytestdb mytable --where=datetime LIKE '2014-09%' 这就是我正在做的事情,它会返回: mysqldum
我正在制作销售税计算器,除了总支付金额部分外,其他一切都正常。在我的程序中,我希望能够输入一个数字并获得该项目的税额我还希望能够获得支付的总金额,包括交易中的税金。到目前为止,我编写的代码完成了所有这
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许在 Stack Overflow 上提出有关通用计算硬件和软件的问题。您可以编辑问题,使其成为
我是否必须进行任何额外的设置才能让 apache-airflow 在任务失败时向我发送电子邮件。我的配置文件中有以下内容(与默认值保持不变): [email] email_backend = airf
这个问题在这里已经有了答案: What does the $ symbol do in VBA? (5 个回答) 3年前关闭。 使用返回字符串(如 Left)的内置函数有什么区别吗?或使用与 $ 相同
我有一个用VB6编写的应用程序,我需要使用一个用.NET编写的库。有什么方法可以在我的应用程序上使用该库吗? 谢谢 最佳答案 这取决于。您可以控制.NET库吗? 如果是这样,则可以修改您的库,以便可以
当我创建一个以 ^ 开头的类方法时,我尝试调用它,它给了我一个错误。 class C { method ^test () { "Hi" } } dd C.new.test; Too m
我已经使用 bower 安装了 angularjs 和 materialjs。 凉亭安装 Angular Material 并将“ngMaterial”注入(inject)我的应用程序,但出现此错误。
我是一名优秀的程序员,十分优秀!