JAVA Programming Exercises foundation program [1] rabbit is not death

 

1 1 [program] is not death rabbit

Title: Classical problem: a pair of rabbits from the first 3 months after birth are born every month one pair of rabbits, bunnies grow up to the third month after the month gave birth to one pair of rabbits, if the rabbit is not dead, ask how much each month as the number of rabbits?

Program analysis: number of rabbits column 1,1,2,3,5,8,13,21 law ....

 

package cskaoyan;

import org.junit.Test;

public class cskaoyan1 {

	// 递归
	@Test
	public void show1() {
		int month = 12;
		System.out.println("第" + month + "个月兔子对数为" + getRabbitNumber1(12));
	}

	public int getRabbitNumber1(int month) {
		if (month == 1 || month == 2) {
			return 1;
		} else {
			return getRabbitNumber1(month - 1) + getRabbitNumber1(month - 2);
		}
	}

	// 非递归
	@Test
	public void show2() {
		int month = 12;
		getRabbitNumber2(month);
	}

	public void getRabbitNumber2(int month) {
		int number1 = 1;
		number2. 1 = int; 
		int count = 0;
		for (int I =. 1; I <= month The; I ++) { 
			IF (I == I == 2 ||. 1) { 
				System.out.println ( "first" + i + "rabbit months on the number of "+ number1); 
			} the else { 
				COUNT = number1 + number2; 
				number2 = number1; 
				number1 = COUNT; 
				System.out.println (" first "+ i +" is the number of months rabbit "+ COUNT); 
			} 
		} 
	} 
}

 

Guess you like

Origin www.cnblogs.com/denggelin/p/11261699.html