首页 > 代码库 > *循环-18. 龟兔赛跑

*循环-18. 龟兔赛跑

 1 /* 2  * Main.c 3  * C18-循环-18. 龟兔赛跑 4  *  Created on: 2014年8月4日 5  *      Author: Boomkeeper 6  ******部分通过******* 7  */ 8  9 #include <stdio.h>10 #include <stdlib.h>11 12 int t = 0;13 int distanceR = 0, distanceT = 0;14 //int *pdistanceR = &distanceR, *pdistanceT = &distanceT;15 16 void competition() {17 18     //兔子的控制器,如果flag=0,可以继续跑并且比较与乌龟的距离,否则直至count满30才可以继续19     int flag = 0, count = 0;20     int i;21     for (i = 1; i <= t; i++) {22 23         distanceT += 3;24 25         if (flag == 0) {26 27             distanceR += 9;28 29             //每10分钟兔子回头看,如果超过乌龟就休息30分钟,否则继续跑10分钟30             if (i % 10 == 0) {31                 if (distanceR > distanceT)32                     flag = 1;33                 else34                     continue;35             }36         }37 38         if (flag == 1) {39             count++;40             if (count == 30){41                 flag = 0;42                 count = 0;43             }44 45         }46 47     }48 }49 50 int main(void) {51 52     scanf("%d", &t);53     competition();54     if (distanceR > distanceT)55         printf("^_^ %d\n", distanceR);56     if (distanceR < distanceT)57         printf("@_@ %d\n", distanceT);58     if (distanceR == distanceT)59         printf("-_- %d\n", distanceR);60     return 0;61 }

 

题目链接:

http://pat.zju.edu.cn/contests/basic-programming/%E5%BE%AA%E7%8E%AF-18

 

 

 

 

   .