关于刚才一只小鸟的问题,请大家看看

stormxs 2009-12-29 02:24:25

class Program
{
static double h = 100; //两地总距离
static double s = 0; //小鸟飞行总距离
const int birdSpeed = 30; //小鸟飞行速度
const int aSpeed = 20; //火车速度A
const int bSpeed = 15; //火车速度B

static void Main(string[] args)
{
for (; ; )
{
double time = h / (birdSpeed + aSpeed); //第一次小鸟与火车相遇所用时间
h = h - aSpeed * time + bSpeed * time; //相遇后两火车之间距离
if (h < 0.1) //判断是否相遇
{
Console.WriteLine(s);
break;
}
else
{
s = s + birdSpeed * time; //没有相遇,累计小鸟飞行距离
time = h / (birdSpeed + bSpeed); //飞向另一火车所用时间
h = h - aSpeed * time + bSpeed * time; //剩余距离
if (h < 0.1)
{
Console.WriteLine(s);
break;
}
else s = s + birdSpeed * time;
}
}
Console.ReadKey();
}
}

得出的总飞行距离不对. 可能是思路不对吧 . 刚才学不久别笑我啊
...全文
120 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
stormxs 2009-12-29
  • 打赏
  • 举报
回复

class Program
{
static double h = 100;
const int birdSpeed = 30;

static void Main(string[] args)
{
double s = 0;
int count = 0;
int aSpeed = 20;
int bSpeed = 15;
int sumSpeed = aSpeed + bSpeed;

while (h > 0.01)
{
double time = h / (birdSpeed + aSpeed);
h = h - (aSpeed + bSpeed) * time;
s = s + birdSpeed * time;
count++;

aSpeed = sumSpeed - aSpeed;
bSpeed = sumSpeed - bSpeed;
}
Console.WriteLine(s);
Console.WriteLine(count);
Console.ReadKey();
}
}

谢谢楼上的. 我一开始没看明白. 后来仔细看了下,重写了,次数是用普通加减是得不到,总有个精度问题
vrhero 2009-12-29
  • 打赏
  • 举报
回复
你说是这个?http://topic.csdn.net/u/20090501/01/77bc8798-90b0-4877-86a8-fc924e26bc34.html

题目都有问题你还真去做啊...这么跟你说吧,小鸟没有长度也没有给出最小步进单位,那就是线性移动了...得用线性代数和微积分求解,不是简单的加减乘除能解决的...
stormxs 2009-12-29
  • 打赏
  • 举报
回复
顶顶了. 大家帮忙看看

13,190

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 分析与设计
社区管理员
  • 分析与设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧