兔子java
① 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);
}
}