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);
}
}
}