龟兔赛跑java代码 java模拟龟兔赛跑的算法思想
求java龟兔赛跑程序代码!
import java.util.*;
10多年的黄岩网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销的优势是能够根据用户设备显示端的尺寸不同,自动调整黄岩建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“黄岩网站设计”,“黄岩网站推广”以来,每个客户项目都认真落实执行。
public class Test{
public static void main(String[] args)throws Exception {
float t,g,m=0,num=0;
float tt,gg;
int q=0;
Scanner s;
System.out.println("输入兔子跑一圈时间/秒:");
s=new Scanner(System.in);
t=s.nextFloat();
System.out.println("输入乌龟跑一圈时间/秒:");
s=new Scanner(System.in);
g=s.nextFloat();
tt=(float)1/(t*1000);
gg=(float)1/(g*1000);
System.out.println("赛跑开始……");
while(true){
try{
Thread.sleep(10);
}catch(Exception e){}
m+=10;
if((int)((tt-gg)*m)q){
q=(int)((tt-gg)*m);
System.out.println("在第"+m/1000+"秒");
System.out.println("兔子超过乌龟"+q+"圈");
}
}
}}
java算法题——龟兔赛跑
public class Test1{
public static void main(String[] args) throws InterruptedException {
int v1=25,v2=10,t=20,s=3,l=100;
Scanner scanner=new Scanner(System.in);
v1=scanner.nextInt();
v2=scanner.nextInt();
t=scanner.nextInt();
s=scanner.nextInt();
l=scanner.nextInt();
int sum1=0,sum2=0;
boolean stop=false;
int stopcount=0;
int i=0;
for ( i = 0; i l/v2; i++) {
if(sum1=l||sum2=l)//如果有一个跑到了终点就结束了
break;
if(stopcount==s)
stop=false; //如果休息的时间够了,就开始跑
if(sum1-sum2=t)
stop=true; //如果超过了t米,就休息
if(!stop)
sum1+=v1; //当兔子不休息的时候跑
else
stopcount++; //休息的时间计数
sum2+=v2;//乌龟每次都会跑
System.out.print("兔子跑了:"+sum1+"米");
System.out.println("乌龟跑了:"+sum2+"米");
}
if(sum1==sum2)
System.out.println("D"+i);
else if(sum1=l)
System.out.println("R"+i);
else if(sum2=l)
System.out.println("T"+i);
}
}
java龟兔赛跑求简单多线程
public class Competition {
private volatile static boolean gameOver = false;//用来标记是否有人到达终点,到达终点后游戏结束
//乌龟的实现方式
static class Tortoise implements Runnable{
private volatile int total = 0;//用来记录当前已经前行了多少距离
@Override
public void run() {
while(!gameOver){
int step = (int)(Math.random()*5+1);//产生1-5的随机数
total += step;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int getTotal(){
return total;
}
}
//兔子的实现方式
static class Rabbit implements Runnable{
private volatile int total = 0;
@Override
public void run() {
while(!gameOver){
int step = (int)(Math.random()*5+1);
total += step;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int getTotal(){
return total;
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final Tortoise tortoise = new Tortoise();
final Rabbit rabbit = new Rabbit();
new Thread(tortoise).start();
new Thread(rabbit).start();
//下面多起了一个线程,相当于比赛的时候的裁判员,他每隔一段时间就看一下是否有人到达终点,若有人到达则宣判该人获胜,游戏结束
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
while(!gameOver){
int torLen = tortoise.getTotal();//获得乌龟前行的距离
int rabLen = rabbit.getTotal();//获得兔子前行的距离
System.out.println("乌龟:"+torLen+",兔子"+rabLen);
if(torLen = 100 rabLen 100){
System.out.println("乌龟获胜!!!");
gameOver = true;
}else if(rabLen = 100 torLen 100){
System.out.println("兔子获胜!!!");
gameOver = true;
}else if(rabLen =100 torLen = 100){//这里有可能两人同时到达终点
System.out.println("同时到达终点!!!");
gameOver = true;
}
try {
Thread.sleep(210);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
JAVA编程题龟兔赛跑
class Animal {
public double speed;
public void run(int length) {
System.out.println(length/this.speed);
}
}
class Rabbit extends Animal {
Rabbit(int speed) {
super.speed = speed;
}
@Override
public void run(int length) {
System.out.println("Rabbit time = "+length/this.speed +" seconds");
}
}
class Tortoise extends Animal {
Tortoise(int speed) {
super.speed = speed;
}
@Override
public void run(int length) {
System.out.println("Tortoise time = "+length/this.speed +" seconds");
}
}
public class Match {
public static int length = 100;
private static void begin(Rabbit r,Tortoise t) {
r.run(length);
t.run(length);
}
public static void main(String[] args) {
Rabbit r = new Rabbit(20);
Tortoise t = new Tortoise(5);
begin(r,t);
}
}
名称栏目:龟兔赛跑java代码 java模拟龟兔赛跑的算法思想
分享URL:http://myzitong.com/article/ddjpjph.html