當前位置:首頁 » 編程語言 » 兔子java

兔子java

發布時間: 2024-07-15 18:01:34

java紼嬪簭鍒嗘瀽錛 鍏斿瓙鐨勮勫緥涓烘暟鍒1,1,2,3,5,8,13,21....

public class Test {//鐢ㄩ掑綊娉曡$畻鍏斿瓙鐨勬彙瀛欒勫緥
static long fib(int x){
if(x>鏃忎俊2) return (fib(x-1)+fib(x-2));
else return 1;
}
public static void main(String[] args) {
for(int i=1;i<=24;i++){
long n=fib(i);
//綆楀厗鐨杞鍑虹殑鏄瀵規暟.瑕佺畻鎬繪暟鐨勬硶,*2灝辮
System.out.println("絎"+i+"涓鏈堟湁鍏斿瓙瀵規暟涓"+n);
}
}
}

② 楦 鍜 鍏斿瓙 涓鍏辨槸 =48 涓錛 浠栦滑鐨勮吙鑴氫竴鍏辨槸 =108 涓錛 璇烽棶 楦=錛 鍏斿瓙=錛熸眰Java閲屾庝箞綆楋紵鏁板﹂噷鍛錛

鏁板︼細


鍋囪撅紝鍏ㄦ槸鍏旓紝鍒錛

4x48=192鏉¤吙銆


姣斿疄闄呭氬嚭浜嗭細

192-108=84鏉¤吙銆


騫朵笖錛屽厰姣旈浮澶2鏉¤吙錛

4-2=2


鎵浠ワ紝鐩鍜愮敤澶氬嚭鐨勮吙鍘婚櫎2錛屽氨鏄楦$殑鏁扮洰浜嗐

楦=84/2=42鍙.


騫朵笖錛屼粠棰樼洰鍙鐭ラ亾錛屼竴鍏遍浮鍏48鍙銆傚嚡鍗栫函鐜板湪宸茬粡鐭ラ亾楦℃槸42鍙錛岀敤48-42錛屽垯錛

鍏=48-42=6鍙


浠g爜錛


publicclassTest

{

publicstaticvoidmain(String[]args)

{

閰嶆槍

for(intchicken=0;chicken<48;chicken++)

{

intrabbit=48-chicken;

if(chicken*2+rabbit*4==108)

{

System.out.println("楦$殑鏁伴噺:"+chicken+"鍏旂殑鏁伴噺錛"+rabbit);

}

}

}

}


③ 用java模擬龜兔賽跑,能設置比賽距離。在任意時間段內兔子的速度是隨機的,烏龜的速度是恆定的。

package test;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
public class Running {
public static int distance;
private static final CountDownLatch CDL = new CountDownLatch(1);
public static final void start(Animal...animals) {
if (animals != null && animals.length > 0) {
Stream.of(animals).forEach(a -> {
Thread th = new Thread(a);
th.start();
});
// register observers to each animal
for (Animal a: animals) {
for (Animal a2: animals) {
a.addObserver(a2);
}
}
CDL.countDown();
System.out.println("Go!!!!!!");
}
}
public static abstract class Animal extends Observable implements Runnable, Observer {
protected final String name;
protected int speed; // m/s
protected boolean randomSpeed;
private int remaining;
private int speedLimitation;
private Random ran;
private ScheledExecutorService es = Executors.();
protected Animal(final String name, final int speedLimitation) {
this.randomSpeed = true;
this.speedLimitation = speedLimitation;
this.ran = new Random();
this.name = name;
}
protected Animal(final int speed, final String name) {
this.name = name;
this.speed = speed;
}
@Override
public void run() {
try {
CDL.await();
} catch (InterruptedException e) {
}
es.scheleAtFixedRate(() -> {
if (randomSpeed) {
int currentSpeed = ran.nextInt(speedLimitation);
if (currentSpeed > speed) {
System.out.println(name + " 開始加速.");
} else if (speed < currentSpeed) {
System.out.println(name + " 慢了下來.");
} else {
System.out.println(name + " 繼續前進.");
}
speed = currentSpeed;
}
if (remaining == 0) {
remaining = distance - speed;
} else {
remaining -= speed;
}
System.out.println(name + " 距離終點還有" + remaining + "米.");
if (remaining <= speed) {
setChanged();
notifyObservers(name);
}
}, 0, 1, TimeUnit.SECONDS);
}
@Override
public void update(Observable o, Object arg) {
String name = (String) arg;
es.shutdownNow();
if (!this.name.equals(name))
System.out.println(name + " 停止了繼續奔跑.");
else
System.out.println(name + " 贏得了比賽!");
}
}
public static void main(String[] args) {
Running.distance = 100;
Running.start(new Rabbit("小兔兔", 5), new Rabbit("小龜龜", 2));
}
}
class Rabbit extends Running.Animal {
protected Rabbit(String name, int speedLimitation) {
super(name, speedLimitation);
}
}
class Tortoise extends Running.Animal {
protected Tortoise(int speed, String name) {
super(speed, name);
}
}

熱點內容
混沌珠演算法 發布:2024-11-26 08:29:17 瀏覽:163
阿里雲解析不到伺服器 發布:2024-11-26 07:57:59 瀏覽:493
python矩陣的行數 發布:2024-11-26 07:56:25 瀏覽:355
街頭籃球手游腳本 發布:2024-11-26 07:51:18 瀏覽:752
閃迪加密器 發布:2024-11-26 07:44:08 瀏覽:553
我的世界伺服器顯示村民名字 發布:2024-11-26 07:37:16 瀏覽:479
php注冊與登錄 發布:2024-11-26 07:31:21 瀏覽:796
基金賬戶如何配置 發布:2024-11-26 07:29:58 瀏覽:181
用電腦怎麼刷汽車行車電腦配置 發布:2024-11-26 07:24:14 瀏覽:689
客戶端ip和伺服器ip地址怎麼設置 發布:2024-11-26 07:18:25 瀏覽:685