方案一:直接算龟兔跑的总路程,然后最后计算超出的路程,感觉这样比较简单。
#include<stdio.h>
int main(void)
{
int s1=0,s2=0;//s1乌龟,s2兔子
int wait=1;//wait是最后一次是乌龟跑了还是乌龟兔子都跑了
int t,T;
t=0;
scanf("%d",&T);
while(t<T){
if(s1>s2){
s2+=3*30;
t+=30;
wait=0;
}else{
s1+=9*10;
s2+=3*10;
t+=10;
wait=1;
}
}
if(t>T){//这是判断最后一次有没有等
if(wait){//最后一次都跑了
s2-=(t-T)*9;
s1-=(t-T)*3;
}else{//最后一次只有乌龟跑了
s2-=(t-T)*3;
}
}
if(s1>s2)printf("^_^ %d",s1);
else if(s2>s1)printf("@_@ %d",s2);
else printf("-_- %d",s1);
return 0;
}
方案二:利用rest来表示状态,同时把兔子时间与外界时间分离,实现兔子休息。
#include<stdio.h>
int main(void)
{
int T;
int t=0;
int s1=0,s2=0;
int rtime=0;//兔子的时间
int rest=0;//兔子是否在休息的状态
scanf("%d",&T);
while(t!=T){
s2+=3;//乌龟跑
if(!rest){//如果兔子不在休息
s1+=9;//兔子跑
rtime++;//兔子时间+
}
t++;
if(t%10==0&&!rest){
if(s2>s1){//如果兔子在乌龟前面
rest=1;//休息
rtime+=30;//兔子时间+30
}
}
if(t==rtime){//时间赶上兔子时间
rest=0;//兔子退出休息
}
}
if(s1>s2) printf("^_^ %d",s1);
else if(s2>s1) printf("@_@ %d",s2);
else printf("-_- %d",s1);
return 0;
}