⑴ java算法题——龟兔赛跑
packagep1;
importjava.util.Scanner;
/**
*龟兔赛跑
*
*@authoryugi
*/
publicclassTortoiseHare
{
publicstaticfinalStringT="T";
publicstaticfinalStringR="R";
publicstaticfinalStringD="D";
privatestaticvoidrace(intv1,intv2,intt,ints,intl,
intstart1,intstart2,ints1,ints2,inttime,
booleanrest)
{
if(
v1<0||v2<0||t<0||s<0||l<0
||v1>100||v2>100||t>300||s>10||l>10000
||l%v1!=0||l%v2!=0
)
{
try
{
thrownewException("参数输入不合标准!");
}
catch(Exceptione)
{}
}
else
{
//兔子或乌龟已经到达终点
if(s1>=l||s2>=l)
{
if(s1>s2)
{
System.out.println(R);
System.out.println(time+start1);
}
elseif(s1<s2)
{
System.out.println(T);
System.out.println(start2);
}
else
{
System.out.println(D);
System.out.println(start2);
}
return;
}
//兔子开始休息
if(!rest&&s1-s2>=t)
{
rest=true;
time+=s;
}
//兔子休息了s秒(不包括s秒,第s秒开始行动,题目意思不是s秒之后)
if(rest&&start2-start1>=s)
{
//开始跑了(不休息)
rest=false;
}
//兔子不休息的时候跑
if(!rest)
{
start1++;
s1=v1*start1;
}
//乌龟总在跑
start2++;
s2=v2*start2;
//递归调用
race(v1,v2,t,s,l,start1,start2,s1,s2,time,rest);
}
}
publicstaticvoidmain(String[]args)
{
System.out.println("输入只有一行,包含用空格隔开的五个正整数v1,v2,t,s,l,"+
" 其中(v1,v2<=100;t<=300;s<=10;l<=10000且为v1,v2的公倍数)"+
" 退出输入n/N,否则继续:");
Scannerscanner=newScanner(System.in);
Stringline=null;
Stringreg="^([1-9]\d*|\d)\s+([1-9]\d*|\d)\s+([1-9]\d*|\d)\s+([1-9]\d*|\d)\s+([1-9]\d*|\d)$";
while(scanner.hasNextLine())
{
line=scanner.nextLine().trim();
if("n".equalsIgnoreCase(line))
{
break;
}
if(!line.matches(reg))
{
System.err.println("输入不对,重新输入:");
}
else
{
String[]input=line.split("\s+");
intv1=Integer.parseInt(input[0]);
intv2=Integer.parseInt(input[1]);
intt=Integer.parseInt(input[2]);
ints=Integer.parseInt(input[3]);
intl=Integer.parseInt(input[4]);
race(v1,v2,t,s,l,0,0,0,0,0,false);
System.out.println("退出输入n/N,否则继续:");
}
}
scanner.close();
}
}
⑵ java模拟龟兔赛跑问题用多线程实现
import java.util.Date;
public class Test extends Thread{ private int tortoise_walk = 0; // 乌龟已跑长度存放变量
private int rabbit_walk = 0; // 兔子已跑长度存放变量
private int finish = 1000; // 终点
private volatile boolean hasWinner = false;// 胜利者诞生 /**
*
* @ClassName: Tortoise_Run
* @Description: TODO(乌龟奔跑线程)
* @author guotingchao
* @date 2012-3-6 上午10:20:45
*
*/
class Tortoise_Run implements Runnable {
@Override
public void run() {
try {
while (!hasWinner) {
if (tortoise_walk % 100 == 0 && (tortoise_walk != 0||tortoise_walk>=finish)) { //乌龟每100米休息500毫秒
System.out.println("乌龟休息中………………");
Thread.sleep(500);
}
tortoise_walk++;
System.out.println("乌龟已跑"+tortoise_walk+"米");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} /**
*
* @ClassName: Rabbit_Run
* @Description: TODO(兔子奔跑线程)
* @date 2012-3-6 上午10:25:10
* @author guotingchao
*/
class Rabbit_Run implements Runnable {
@Override
public void run() {
try {
while (!hasWinner) {
if (rabbit_walk % 20 == 0 && (rabbit_walk != 0||rabbit_walk>=finish)) { //兔子每20米休息500毫秒
System.out.println("兔子休息中………………");
Thread.sleep(500);
}
rabbit_walk=rabbit_walk+5; //每秒跑5步
System.out.println("兔子已跑"+rabbit_walk+"米");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void run(){
new Thread(new Rabbit_Run()).start();
new Thread(new Tortoise_Run()).start();
}
/**
* @Title: main
* @Description: TODO(
* 赛程1000米,兔子跑5米,乌龟跑1米,兔子每20米休息500毫秒,乌龟每100米休息500毫秒。谁先到终点就结束程序
* ,并显示获胜方。)
* @param @param args
* @param @throws Exception 设定文件
* @author guotingchao
* @return void 返回类型
* @throws
*/
public static void main(String[] args) throws Exception {
long temp_actionTime=System.currentTimeMillis();
System.out.println("比赛开始:"+new Date(temp_actionTime)+"毫秒");
Test t=new Test();
new Thread(t).start();
while(true){
if(t.tortoise_walk>=t.finish||t.rabbit_walk>=t.finish){
t.hasWinner=true;
break;
}
}
String winnnerName=t.tortoise_walk>t.rabbit_walk?"乌龟":"兔子";
long temp_lastTime=System.currentTimeMillis();
System.out.println(winnnerName+"胜利");
System.out.println("比赛结束:"+new Date(temp_lastTime)+"毫秒");
System.out.println("所耗时间:"+(temp_lastTime-temp_actionTime)+"毫秒");
System.out.println("兔子="+t.rabbit_walk+" 乌龟="+t.tortoise_walk);
}
}
//不知道兔子和乌龟的步长时间是否按每秒。 这里程序只考虑依次递增频率
⑶ JAVA的程序设计,设计一个龟兔赛跑的线程类模拟参与赛跑。
感觉挺有趣的,试着写了个~
public static void main(String[] arg) {
new wugui().run();
new tuzi().run();
}
static class wugui {
final int su = 4;// 乌龟的速度是每秒4米
public static boolean hasEnd = false;// 是否已经跑到终点
public void run() {
new Thread() {
public void run() {
int distance = 0;
while (distance < 100) {
try {
Thread.sleep(1000);
distance += su;
System.out.println("小乌龟跑了" + distance + "米");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
hasEnd = true;
if (tuzi.hasEnd) {
System.out.println("呜呜,差一点点就赢了~");
} else {
System.out.println("胜利是属于有准备的人的,你的自大害了你!-------乌龟赢了");
}
}
}.start();
}
}
static class tuzi {
final int su = 5;// 兔子的速度是每秒5米
public static boolean hasEnd = false;// 是否已经跑到终点
public void run() {
new Thread() {
@Override
public void run() {
int distance = 0;// 跑了多少米
boolean hasXiuXi = false;// 是否休息过
while (distance < 100) {
try {
Thread.sleep(1000);
distance += su;
System.out.println("小兔子跑了" + distance + "米");
if (distance > 50 && !hasXiuXi) {
System.out.println("小兔子累了,决定休息一会儿~");
Thread.sleep((long) (10000 * Math.random()));
System.out.println("小兔子休息够了,又开始跑了,决一胜负吧!");
hasXiuXi = true;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
hasEnd = true;
if (wugui.hasEnd) {
System.out.println("呜呜,早知道就不休息了~");
} else {
System.out.println("哇哈哈,你个战5渣也想赢我~~做梦去吧!!-------兔子赢了");
}
}
}.start();
}
}