乌龟与兔子进行赛跑,跑场是一个矩型跑道,跑道边可以随地进行休息。乌龟每分钟可以前进3米,兔子每分钟前进9米;兔子嫌乌龟跑得慢,觉得肯定能跑赢乌龟,于是,每跑10分钟回头看一下乌龟,若发现自己超过乌龟,就在路边休息,每次休息30分钟,否则继续跑10分钟;而乌龟非常努力,一直跑,不休息。假定乌龟与兔子在同一起点同一时刻开始起跑,请问T分钟后乌龟和兔子谁跑得快?
输入格式:
输入在一行中给出比赛时间T(分钟)。
输出格式:
在一行中输出比赛的结果:乌龟赢输出@_@
,兔子赢输出^_^
,平局则输出-_-
;后跟1空格,再输出胜利者跑完的距离。
#include <iostream>
//龟兔赛跑
using namespace std;
int m(int a)
{
int p=3*a;
return p;
}
int n(int a)//兔行距离 分段函数
{
int q;
if (a<10)
q=9*a;
else if (a<40)
q=90;
else if (a<50)
q=90+9*(a-40);
else if (a<80)
q=180;
else if (a<90)
q=180+9*(a-80);
else
q=270*(a/90)+n(a%90);//递归一下
return q;
}
int main()
{
int t;
cin>>t;
if(m(t)>n(t))
cout<<"@_@"<<" "<<m(t);
else if(m(t)==n(t))
cout<<"-_-"<<" "<<n(t);
else
cout<<"^_^"<<" "<<n(t);
return 0;
}
运行结果应该是没有问题,但是不知道为什么一直拿不到满分,先存下来后面再看吧。
2017/2/24发现原来是漏掉了平局情况。