PTA
7-39 龟兔赛跑
1 #include <stdio.h> 2 3 int main () { 4 5 int rabbit=0,turtle=0,minute,rest=-1,run=10; //rest为0或负时,兔子休息,run为1-10是兔子跑 6 scanf ("%d",&minute) ; 7 8 while(minute--){ 9 turtle += 3; 10 if (run-- > 0) 11 rabbit += 9; 12 if (run == 0 ){ //兔子跑10分钟回头看一下,如果比乌龟快就休息30分钟,反之再跑10分钟 13 if (rabbit > turtle) 14 rest = 30; 15 else run = 10 ; 16 } 17 if (rest-- == 0) //休息结束继续跑10分钟 18 run = 10; 19 } 20 //printf("rabbit=%d,turtle=%d\n",rabbit,turtle); 21 if(turtle > rabbit) 22 printf("@_@ %d",turtle); 23 else if (rabbit > turtle) 24 printf("^_^ %d",rabbit); 25 else 26 printf("-_- %d",rabbit); 27 28 return 0 ; 29 }
我的程序:
1 #include<stdio.h> 2 int main() { 3 int sleep=0,clock=-1,T,t=0,s1=0,s2=0; 4 scanf("%d",&T); 5 while(T--){ 6 if(sleep==1 && clock==0) sleep = 0; //闹钟响,起床 7 if(t%10==0 && sleep==0 && s2>s1){ 8 sleep = 1;//睡觉 9 clock = 30;//闹钟 10 } 11 s1 += 3; 12 if(sleep==0) s2 += 9;//不睡就跑 13 if(sleep==1) clock--; 14 t++; 15 } 16 if(s1>s2) printf("@_@ %d",s1); 17 else if(s2>s1) printf("^_^ %d",s2); 18 else printf("-_- %d",s1); 19 }
几个细节要考虑清楚:
1、t从0开始
2、while循环中是1s中发生的事情,按先后次序执行
3、sleep:睡觉状态,clock:计时