当前位置:   article > 正文

PTA基础编程题-C语言-7-22 龟兔赛跑_c#龟兔赛跑让兔子暂停乌龟继续奔跑

c#龟兔赛跑让兔子暂停乌龟继续奔跑

/*
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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/64456
推荐阅读
相关标签
  

闽ICP备14008679号