7-9 龟兔赛跑 (20分)

这篇文章探讨了乌龟与兔子的经典赛跑问题,加入了兔子的休息策略,分析两者在给定时间下的赛果。通过计算和模拟,揭示了比赛的胜负及距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

7-9 龟兔赛跑 (20分)

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

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

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

输入样例:
242
输出样例:
@_@ 726

c语言:

#include<stdio.h>
int main()
{
	int t,i,j=0,sleep=0,tz=0,wg=0,flag=0;
	scanf("%d",&t);
	for(i=1;i<=t;i++)
	{
		if(flag==1) 
		{
			sleep++;
			if(sleep==30) flag=0;
		}
		else 
		{
			j++;
			tz=tz+9;
			if(j==10)
			{
				if(tz>wg)
				{
					flag=1;
					sleep=0;
				}
				j=0;
			}
		}
		wg=wg+3;
	}
	if(wg==tz) printf("-_- %d\n");
	else if(tz<wg) printf("@_@ %d\n",wg);
	else printf("^_^ %d\n",tz);
	return 0;
}

Java:

待更新
好的,以下是一个基于Java图形用户界面(GUI)的龟兔赛跑小程序。这个程序使用Java Swing库来创建GUI界面,还使用了多线程来模拟比赛。 ``` import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class TortoiseAndHareRaceGUI extends JFrame implements ActionListener, Runnable { private static final long serialVersionUID = 1L; private static final int WIDTH = 800; private static final int HEIGHT = 600; private static final int FINISH_LINE = 700; private static final int TORTOISE_START_POSITION = 50; private static final int HARE_START_POSITION = 50; private static final int MOVE_PIXELS = 10; private JPanel raceTrackPanel; private JLabel tortoiseLabel; private JLabel hareLabel; private JButton startButton; private Thread raceThread; private int tortoisePosition; private int harePosition; private boolean raceInProgress; public TortoiseAndHareRaceGUI() { setTitle("Tortoise and Hare Race"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); tortoisePosition = TORTOISE_START_POSITION; harePosition = HARE_START_POSITION; raceInProgress = false; setupGUI(); } private void setupGUI() { // Race track panel raceTrackPanel = new JPanel() { private static final long serialVersionUID = 1L; @Override protected void paintComponent(Graphics g) { super.paintComponent(g); // Draw starting line g.setColor(Color.BLACK); g.fillRect(0, 100, getWidth(), 5); // Draw finishing line g.setColor(Color.RED); g.fillRect(FINISH_LINE, 0, 5, getHeight()); // Draw tortoise g.setColor(Color.GREEN); g.fillRect(tortoisePosition, 50, 50, 50); // Draw hare g.setColor(Color.BLUE); g.fillRect(harePosition, 200, 50, 50); } }; raceTrackPanel.setLayout(new BorderLayout()); add(raceTrackPanel, BorderLayout.CENTER); // Labels panel JPanel labelsPanel = new JPanel(new GridLayout(2, 1)); tortoiseLabel = new JLabel("Tortoise: " + TORTOISE_START_POSITION); tortoiseLabel.setFont(new Font("Arial", Font.BOLD, 20)); labelsPanel.add(tortoiseLabel); hareLabel = new JLabel("Hare: " + HARE_START_POSITION); hareLabel.setFont(new Font("Arial", Font.BOLD, 20)); labelsPanel.add(hareLabel); add(labelsPanel, BorderLayout.NORTH); // Start button startButton = new JButton("Start"); startButton.addActionListener(this); add(startButton, BorderLayout.SOUTH); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == startButton) { if (!raceInProgress) { startRace(); } } } private void startRace() { raceInProgress = true; startButton.setEnabled(false); raceThread = new Thread(this); raceThread.start(); } private void endRace() { raceInProgress = false; startButton.setEnabled(true); // Determine winner if (tortoisePosition >= FINISH_LINE && harePosition >= FINISH_LINE) { setTitle("It&#39;s a tie!"); } else if (tortoisePosition >= FINISH_LINE) { setTitle("Tortoise wins!"); } else { setTitle("Hare wins!"); } } @Override public void run() { Random random = new Random(); while (tortoisePosition < FINISH_LINE && harePosition < FINISH_LINE) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // Tortoise&#39;s move int tortoiseMove = random.nextInt(10) + 1; if (tortoiseMove >= 1 && tortoiseMove <= 5) { // Fast plod tortoisePosition += MOVE_PIXELS * 3; } else if (tortoiseMove >= 6 && tortoiseMove <= 7) { // Slip tortoisePosition -= MOVE_PIXELS * 6; } else { // Slow plod tortoisePosition += MOVE_PIXELS; } if (tortoisePosition < 0) { tortoisePosition = 0; } // Hare&#39;s move int hareMove = random.nextInt(10) + 1; if (hareMove >= 1 && hareMove <= 2) { // Sleep } else if (hareMove >= 3 && hareMove <= 4) { // Big hop harePosition += MOVE_PIXELS * 9; } else if (hareMove == 5) { // Big slip harePosition -= MOVE_PIXELS * 12; } else if (hareMove >= 6 && hareMove <= 8) { // Small hop harePosition += MOVE_PIXELS; } else { // Small slip harePosition -= MOVE_PIXELS * 2; } if (harePosition < 0) { harePosition = 0; } // Update labels tortoiseLabel.setText("Tortoise: " + tortoisePosition); hareLabel.setText("Hare: " + harePosition); // Repaint race track raceTrackPanel.repaint(); } endRace(); } public static void main(String[] args) { TortoiseAndHareRaceGUI race = new TortoiseAndHareRaceGUI(); race.setVisible(true); } } ``` 这个程序创建了一个GUI界面,包括一个赛道面板、一个标签面板和一个开始按钮。当用户点击开始按钮时,程序会启动一个模拟比赛的线程,并在赛道面板上绘制乌龟兔子的移动轨迹。同时,程序还会更新标签面板上的比赛进程。当比赛结束时,程序会禁用开始按钮,并显示胜者的名字。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值