还想请问一个loop上的运行问题
麻烦问一下这个问题在loop上出不了结果
题目大意是:有一个忍者和海盗对打,两人中间隔着一定距离且中间放着一把刀。忍者可以迅速跑到刀那里并捡起防御海盗的攻击。海盗有一把枪,需要做上膛耗费一定时间。如果海盗上完膛,忍着没拿到刀,则海盗胜。反之可推。
初步我写的pseudo-code
float ninjaSpeed,pirateTime, combatDistance, swordDistance
print "Please enter a speed for the ninja: "
ninjaSpeed <- readInput
print "Please enter a draw and shoot time for the pirate: "
pirateTime <- readInput
print "Please enter a combat distance: "
combatDistance <- readInput
print "Please enter the distance to the sword: "
swordDistance <- readInput
if (swordDistance / ninjaSpeed <=pirateTime)
print "The ninja makes it to his sword. Yay for the ninja!" endline
else
print "The pirate shoots the ninja dead. Yay for teh pirate!" endline
然后就是这道题目的给出的例子了(应当像这样运行):
Please enter the distance between the ninja and the pirate (in meters): 100
How far away is the ninja from his sword (in meters): 20
Please enter the ninja's running speed (meters per second): 9
Please enter the number of seconds it takes the pirate to draw and fire his gun: 3
The ninja starts 20 meters from the sword
The pirate takes 3 seconds to draw and fire his gun.
A
fter 1 second the ninja is 11 meters from the sword and the pirate will draw and fire his gun in 2 seconds.
After 2 seconds the ninja is 2 meters from the sword and the pirate will draw and fire his gun in 1 second.
The ninja makes it to his sword before getting shot.
这是我写的部分
#include <iostream>
using namespace std;
int main()
{
int ninjaSpeed, pirateTime, combatDistance, swordDistance ,time = 0;
cout<<"Please enter the distance between the ninja and the pirate (in meters):";
cin>>combatDistance;
cout<<"How far away is the ninja from his sword (in meters):";
cin>>swordDistance;
cout<<"Please enter the ninja's running speed (meters per second):" ;
cin>>ninjaSpeed;
cout<<"Please enter the number of seconds it takes the pirate to draw and fire his gun:" ;
cin>>pirateTime;
do
{
swordDistance = swordDistance - (ninjaSpeed*time);
pirateTime = pirateTime - time;
time=time+1;
}
while (NswordDistance / ninjaSpeed >=pirateTime);
cout<<"After"<< time << "second the ninja is "<< NswordDistance << "meters from the sword and the pirate will draw and fire his gun in " << NpirateTime<<" seconds"<<endl;
return 0;
}
我loop这里还是不太懂,既然是do-while loop 那么这样写应该会出现例子中给的结果,但我运行的时候却出不来,还请各位多多指点。
[ 本帖最后由 razielzyc 于 2010-10-13 11:51 编辑 ]