[PTA] 7-39 Hare (20 minutes)

Tortoise and hare race carried out, running field is a rectangular runway, runway edge can be anywhere rest. Tortoise can move forward 3 meters per minute, rabbit forward nine meters per minute; hare tortoise too slow runner, that certainly beat the tortoise, so run every 10 minutes to look back at the turtle, found himself more than if the turtle, on the road rest, rest every 30 minutes, or continue to run 10 minutes; and the turtle very hard, has been running, without a break. Assumed the tortoise and the hare began to start at the same starting point the same time, I ask after T minutes tortoise and hare who was faster?

Input format:
input match given time T (min) in a row.

Output formats:
result output matches in a row: The tortoise wins output @ @ rabbit wins output _ , draw the output - -; followed by a space, then output from the finish of the winner.

Input Sample:
242

Sample output:
@ _ @ 726

Act One:
conventional thinking

#include <stdio.h>
int main(){
	int t,stop=0;
	int x=0,y=0;
	int k=0;
	scanf("%d",&t);
	if(t<=10){
		x=3*t;
		y=9*t;
	}else{
		x=30;
		y=90;
		stop=10;
		while(k==0){
			if(x<y){
			if(t-stop<=30){
				x=x+(t-stop)*3;
				k=1;
			}else{
				x=x+90;
				stop=stop+30;
			}
		}else{
			if(t-stop<=10){
				x=x+(t-stop)*3;
				y=y+(t-stop)*9;
				k=1;
			}else{
				x=x+30;
				y=y+90;
				stop=stop+10;
			}
		}
		}	
	}
	if(x>y){
		printf("@_@ %d",x);
	}else if(x==y){
		printf("-_- %d",y);
	}else{
		printf("^_^ %d",y);
	}
	return 0;
} 

Method two:
The main idea is to use the state of rest is determined whether or not the rabbit, and then set a timer on the sub-state of each state, the state used to calculate the elapsed time, and then embedded by judging, to achieve the state conversion

#include<stdio.h>
int main(){
    int r_distance = 0,t_distance = 0,T,t=0,state =1,r_count=0,t_count=0;

    scanf("%d",&T);

    while(t<T){//总计时器从0开始,到T-1为止经过了T分钟
        t++;每分钟进行一次距离加和

        if(state == 1){//兔子跑
                r_count++;
                r_distance += 9;
                t_distance += 3;
                if(r_count == 10){

                    if(r_distance > t_distance){
                        state = 0;//rest for 30 minutes
                        t_count = 0;兔子休息,乌龟计时器初始化
                    }else{
                        state = 1;//run for another 10 minutes
                        r_count = 0;兔子继续跑,兔子计时器初始化
                    }//需要返回确认t是否溢出
                }else;

        }else{//兔子休息
            t_count++;
            t_distance += 3;
            if(t_count == 30){//兔子休息一轮了

                if(r_distance > t_distance){
                    state = 0;//rest for another 30 minutes
                    t_count = 0;兔子继续休息,乌龟计时器初始化
                }else{
                    state = 1;//run for 10 minutes
                    r_count = 0;//兔子开跑,兔子计时器初始化
                }//需要返回确认t是否溢出
            }else;
        }
    }
    if(r_distance > t_distance){
        printf("^_^ %d",r_distance);
    }else if(r_distance < t_distance){
        printf("@_@ %d",t_distance);
    }else{
        printf("-_- %d",t_distance);
    }

    return 0;
}

Method two:
Draw from - time to clear the image
is plotted
in a cycle for 90 minutes, every 90 minutes is equal to, rabbits and ran away tortoise

#include <stdio.h>

int main()
{
	int t;
	scanf("%d", &t);
	int t0 = t % 90;
	if (t0 > 30 && t0 < 45 || t0 > 60 && t0 < 90)
		printf("@_@ %d", t * 3); //乌龟赢
	else if (t0 > 0 && t0 < 30 || t0 > 45 && t0 < 60)
	{ //兔子赢
		int s;
		if (t0 > 0 && t0 < 10)
			s = 9 * t0;
		else if (t0 > 10 && t0 < 30)
			s = 90;
		else if (t0 > 45 && t0 < 50)
			s = 90 + 9 * (t0 - 40);
		else
			s = 180;
		printf("^_^ %d", t / 90 * 270 + s);
	}
	else
		printf("-_- %d", t * 3); //平局
	return 0;
}
Published 48 original articles · won praise 0 · Views 303

Guess you like

Origin blog.csdn.net/weixin_46399138/article/details/105414218