| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 491 人关注过本帖
标题:还想请问一个loop上的运行问题
只看楼主 加入收藏
razielzyc
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2010-10-12
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:1 
还想请问一个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 编辑 ]
搜索更多相关主题的帖子: 运行 loop 
2010-10-13 11:49
shining小南
Rank: 2
等 级:论坛游民
威 望:1
帖 子:47
专家分:42
注 册:2010-9-16
收藏
得分:20 
给你修改了一下,加了一些注释,能得到你的结果,但是你的原理上有bug,eg:pirateTime=3时恰好swordDistance变为0,算谁win.
#include <iostream>

using namespace std;

int main()
{
  int ninjaSpeed, pirateTime, combatDistance, swordDistance ,time = 1;//ninjia武士,pirate海盗,combat战斗,sword刀,time的初值修改为1


  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;// swordDistance 是变化后的值,无须*上time
      pirateTime = pirateTime - 1;// pirateTime每次递减1
      if(swordDistance<0)
      {
          cout<<"The ninja makes it to his sword before getting shot.";
          break;
      }

      cout<<"After"<< time << "second the ninja is "<< swordDistance << "meters from the sword and the pirate will draw and fire his gun in " << pirateTime<<" seconds"<<endl;//加在loop里面
      time=time+1;

    }while (pirateTime>0);//swordDistance值已修改
2010-10-17 13:41
快速回复:还想请问一个loop上的运行问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016571 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved