C语言基础 大佬帮看看

qq_42083940 2018-07-25 07:31:46
7-22 龟兔赛跑(20 分)提问
乌龟与兔子进行赛跑,跑场是一个矩型跑道,跑道边可以随地进行休息。乌龟每分钟可以前进3米,兔子每分钟前进9米;兔子嫌乌龟跑得慢,觉得肯定能跑赢乌龟,于是,每跑10分钟回头看一下乌龟,若发现自己超过乌龟,就在路边休息,每次休息30分钟,否则继续跑10分钟;而乌龟非常努力,一直跑,不休息。假定乌龟与兔子在同一起点同一时刻开始起跑,请问T分钟后乌龟和兔子谁跑得快?

输入格式:
输入在一行中给出比赛时间T(分钟)。

输出格式:
在一行中输出比赛的结果:乌龟赢输出@_@,兔子赢输出^_^,平局则输出-_-;后跟1空格,再输出胜利者跑完的距离。

输入样例:
242
输出样例:
@_@ 726

我的代码:
#include<stdio.h>

int main()
{
int t,x,y;
int temp=0;
scanf("%d",&t);
x=t/40;
y=t%40;
if(y<=10)
temp=6*y-30*x;
else
temp=60-3*(y-10)-30*x;
if(temp>0){
printf("^_^ ");
printf("%d",3*t+temp);
}
else if(temp<0){
printf("@_@ ");
printf("%d",3*t);
}
else{
printf("-_- ");
printf("%d",3*t);
}
return 0;
}

大佬们帮我看看有两个测试点怎么都过不去,不知道出错在哪
...全文
172 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zangfong 2018-07-26
  • 打赏
  • 举报
回复
是你对题意的理解产生了偏差,并非你认为的以40分钟为一个周期。
你可以检查一下,当t = 100时,应该是兔子胜出,但你的程序判断为乌龟胜出。
题目很简单,你可以先用常规思路解决问题,或者先列举前180分钟内每隔10分钟的情况,然后再来找规律。
Votangroom 2018-07-26
  • 打赏
  • 举报
回复
周期是90分钟,LZ自己画图分析一下
Votangroom 2018-07-26
  • 打赏
  • 举报
回复
#include <stdio.h>



int compare(int time)

{

if(time>90)

{

printf("time error");

return 0;

}

else if((time>0&&time<30)||(time>45&&time<60))

{

printf("^_^ ");

return 1;//兔子快

}

else if(time>30&&time<45||(time>60&&time<90))

{

printf("@_@ ");

return 2;//乌龟快

}

else

{

printf("-_- ");

return 3;//一样快

}

}



int howlong(int who,int time,int cir)

{

switch(who)

{

case 1:if(time<=10){return (cir*270+time*9);}

else if(time<=40){return (cir*270+90);}

else if(time<=50){return (cir*270+90+time*9);}

else if(time<=80){return (cir*270+180);}

else if(time<=90){return (cir*270+180+time*9);}

else{printf("time error"); return 0;}

break;

case 2:return (cir*270+time*3);break;

case 3:return (cir*270+time*3);break;

}

}



void main()

{

int time,cir;

printf("how long the time is(min):");

scanf("%d",&time);

cir=time/90;

time%=90;

printf("%d meter\n",howlong(compare(time),time,cir));

}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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