PAT-JAVA-5-22 龟兔赛跑 (20分)

乌龟与兔子进行赛跑,跑场是一个矩型跑道,跑道边可以随地进行休息。乌龟每分钟可以前进3米,兔子每分钟前进9米;兔子嫌乌龟跑得慢,觉得肯定能跑赢乌龟,于是,每跑10分钟回头看一下乌龟,若发现自己超过乌龟,就在路边休息,每次休息30分钟,否则继续跑10分钟;而乌龟非常努力,一直跑,不休息。假定乌龟与兔子在同一起点同一时刻开始起跑,请问T分钟后乌龟和兔子谁跑得快?

输入格式:

输入在一行中给出比赛时间T(分钟)。

输出格式:

在一行中输出比赛的结果:乌龟赢输出@[email protected],兔子赢输出^_^,平局则输出-_-;后跟1空格,再输出胜利者跑完的距离。

输入样例:

242

输出样例:

@[email protected] 726
import java.util.Scanner;
public class Main{
	public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
    	int t = 0 ,g = 0;			//t为兔子跑的距离   g为龟跑的距离
    	int T = sc.nextInt();		//输入时间

    	//一共有4中情况    当时间>0 时循环以下4个
    	// 1.  兔在龟后 (包括距离相等)  时间>10min   兔距离+90  龟距离+30  时间-10;
		// 2.  兔在龟后 (包括距离相等)  时间<10min   兔距离+9*T 龟距离+3*T 时间=0;
		// 3.  兔在龟前   时间>=30min 兔距离+0  龟距离+90 时间-30;
		// 4.  兔在龟前   时间<30min  兔距离+0 龟距离+3*T  时间=0;
    	if(T>10){			

    		while(T>0){

        		if(t<=g&&T>=10){
        			t+=90;
        			g+=30;
        			T-=10;
        		}
        		else if(t>g&&T>=30){
        			t+=0;
        			g+=90;
        			T-=30;
        		}
        		else if(t<=g&&T<10){
        			t+=9*T;
        			g+=3*T;
        			T =0;
        		}
        		else if(t>g&&T<30){
        			t+=0;
        			g+=3*T;
        			T=0;
        		}
        	}

    		//结果的输出
        	if(t<g){
        		System.out.println("@[email protected] "+g);
        	}
        	else if(t>g){
        		System.out.println("^_^ "+t);
        	}
        	else{
        		System.out.println("-_- "+t);
        	}
    	}
    	else if(T==0){			//时间等于0单独进行处理
    		System.out.println("-_- "+t);
    	}
    	else if(T>0&&T<=10){	//时间小于10分钟的时候也单独处理
    		System.out.println("^_^ "+9*T);
    	}
    }
}

时间: 2024-08-10 12:34:29

PAT-JAVA-5-22 龟兔赛跑 (20分)的相关文章

pat 1015 Reversible Primes(20 分)

1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime. Now given a

pat 1042 Shuffling Machine(20 分)

1042 Shuffling Machine(20 分) Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid "inside jobs" where employees collaborate with gamblers by performing

pat 1050 String Subtraction(20 分)

1050 String Subtraction(20 分) Given two strings S?1?? and S?2??, S=S?1???S?2?? is defined to be the remaining string after taking all the characters in S?2?? from S?1??. Your task is simply to calculate S?1???S?2?? for any given strings. However, it

pat 1041 Be Unique(20 分)

1041 Be Unique(20 分) Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1,10?4??]. The first one who bets on a unique number wins. For e

pat 1046 Shortest Distance(20 分) (线段树)

1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input file contains one test case. For

pat 1108 Finding Average(20 分)

1108 Finding Average(20 分) The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [?1000,1000]

pat 1132 Cut Integer(20 分)

1132 Cut Integer(20 分) Cutting an integer means to cut a K digits lone integer Z into two integers of (K/2) digits long integers A and B. For example, after cutting Z = 167334, we have A = 167 and B = 334. It is interesting to see that Z can be devid

PAT乙级1072-----开学寄语 (20分)

1072 开学寄语 (20分) 输入样例: 4 2 2333 6666 CYLL 3 1234 2345 3456 U 4 9966 6666 8888 6666 GG 2 2333 7777 JJ 3 0012 6666 2333 输出样例: U: 6666 6666 GG: 2333 JJ: 6666 2333 3 5 思路:1.用数组下标表示违禁物品编号2.不满4位数要补0,例如:编号12输出时为0012 首次通过代码: 1 #include<stdio.h> 2 3 int main(

PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime. Now given

PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)

1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1]. The first one who bets on a unique number wins. For example