赞
踩
/*
7-22 龟兔赛跑 (20分)
乌龟与兔子进行赛跑,跑场是一个矩型跑道,跑道边可以随地进行休息。乌龟每分钟可以前进3米,兔子每分钟前进9米;兔子嫌乌龟跑得慢,觉得肯定能跑赢乌龟,
于是,每跑10分钟回头看一下乌龟,若发现自己超过乌龟,就在路边休息,每次休息30分钟,否则继续跑10分钟;
而乌龟非常努力,一直跑,不休息。假定乌龟与兔子在同一起点同一时刻开始起跑,请问T分钟后乌龟和兔子谁跑得快?
输入格式:
输入在一行中给出比赛时间T(分钟)。
输出格式:
在一行中输出比赛的结果:乌龟赢输出@@,兔子赢输出_,平局则输出--;后跟1空格,再输出胜利者跑完的距离。
输入样例:
242
输出样例:
@_@ 726
*/
//将时间按10分块
//设置一个状态变量来表示兔子的状态:跑步、休息
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <stdbool.h> //基础编程题目集:38编程题+13函数题 int main() { int T=0; int rabbit_s=0,tortoise_s=0; scanf("%d",&T); bool releax=false; if(T>10) { T=T-10; rabbit_s+=9*10; tortoise_s+=3*10; //以10为单位 while(T>=10) { if(rabbit_s>tortoise_s)//兔子超过了乌龟 {//兔子休息30分钟 releax=true; if(T>=30)//不能保证剩下的时间一定有30分钟 { T=T-30; tortoise_s+=3*30; releax=false;//休息完了继续奔跑 } else { tortoise_s+=3*T;//此时的时间不足30分钟,兔子继续休息 T=0; } } else { T=T-10; rabbit_s+=9*10; tortoise_s+=3*10; releax=true;//兔子跑完10分钟需要休息 } } } if(!releax) rabbit_s+=9*T; tortoise_s+=3*T; if(rabbit_s>tortoise_s) { printf("^_^ %d",rabbit_s); } else if(rabbit_s==tortoise_s) { printf("-_- %d",rabbit_s); } else if(rabbit_s<tortoise_s) { printf("@_@ %d",tortoise_s); } return 0; }
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。