java兔子
⑴ 楦 鍜 鍏斿瓙 涓鍏辨槸 =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编程题龟兔赛跑
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绋嫔簭鍒嗘瀽锛 鍏斿瓙镄勮勫緥涓烘暟鍒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);
}
}
}