PAT基础编程题目-7-22 龟兔赛跑

PAT基础编程题目-7-22 龟兔赛跑

题目详情

题目地址:https://pintia.cn/problem-sets/14/problems/802

解答

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);
	}
}

创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!

全部评论

相关推荐

10-17 12:16
同济大学 Java
7182oat:快快放弃了然后发给我,然后让我也泡他七天最后再拒掉,狠狠羞辱他一把😋
点赞 评论 收藏
分享
10-30 10:16
南京大学 Java
龚至诚:给南大✌️跪了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务