PAT基础编程题目-7-22 龟兔赛跑
PAT基础编程题目-7-22 龟兔赛跑
题目详情
解答
C语言版
#include<stdio.h>
int main() {
int T, tortoise = 0, rabbit = 0, time = 0, flag = 1;
scanf("%d", &T);
while (flag && time < T) {
if (time % 10 == 0 && rabbit > tortoise) {
for (int i = 0; i < 30; i++) {
if (time == T) {
// 兔子休息中结束比赛
flag = 0;
break;
}
tortoise = tortoise + 3;
time++; // 兔子开始休息时,应该是乌龟先跑3米,时间再往后加
}
}
else {
time++;
tortoise = tortoise + 3;
rabbit = rabbit + 9;
}
}
if (tortoise > rabbit)
printf("@_@ %d", tortoise);
else if (tortoise < rabbit)
printf("^_^ %d", rabbit);
else
printf("-_- %d", rabbit); // 平局也要输出距离
return 0;
}
C++版
#include<iostream>
using namespace std;
int main() {
int T, tortoise = 0, rabbit = 0, time = 0, flag = 1;
cin >> T;
while (flag && time < T) {
if (time % 10 == 0 && rabbit > tortoise) {
for (int i = 0; i < 30; i++) {
if (time == T) {
// 兔子休息中结束比赛
flag = 0;
break;
}
tortoise = tortoise + 3;
time++; // 兔子开始休息时,应该是乌龟先跑3米,时间再往后加
}
}
else {
time++;
tortoise = tortoise + 3;
rabbit = rabbit + 9;
}
}
if (tortoise > rabbit)
cout << "@_@ " << tortoise;
else if (tortoise < rabbit)
cout << "^_^ " << rabbit;
else
cout << "-_- " << rabbit; // 平局也要输出距离
return 0;
}
Java版
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int T = 0, tortoise = 0, rabbit = 0, time = 0;
boolean flag = true;
Scanner scanner = new Scanner(System.in);
if (scanner.hasNext())
T = scanner.nextInt();
scanner.close();
while (flag && time < T) {
if (time % 10 == 0 && rabbit > tortoise) {
for (int i = 0; i < 30; i++) {
if (time == T) {
// 兔子休息中结束比赛
flag = false;
break;
}
tortoise = tortoise + 3;
time++; // 兔子开始休息时,应该是乌龟先跑3米,时间再往后加
}
}
else {
time++;
tortoise = tortoise + 3;
rabbit = rabbit + 9;
}
}
if (tortoise > rabbit)
System.out.println("@_@ "+tortoise);
else if (tortoise < rabbit)
System.out.println("^_^ "+rabbit);
else
System.out.println("-_- "+rabbit);
}
}
创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!