C代码
long int get_rabbits_cnt(long int month)
{
long int cnt = 1;
long int cnt_pre = 0;
long int cnt_prepre = 0;
long int i;
for(i = 1; i < month; i++)
{
cnt_prepre = cnt_pre;
cnt_pre = cnt;
cnt = cnt_pre+cnt_prepre;
}
return cnt;
}
java代码
public class HelloWorld {
private static Scanner scan;
public static void main(String []args){
scan = new Scanner(System.in);
long rabbits = 0;
long month = 0;
while(true)
{
System.out.println("输入想查询的月数");
// 判断是否还有输入
if(scan.hasNextLine()){
String str2 = scan.nextLine();
System.out.println("输入的数据为:"+str2);
month = Integer.parseInt(str2);
rabbits = calculate_rabbit(month);
}
System.out.println("month: "+month+" rabbits pair: "+rabbits+" number: "+(rabbits<<1));
}
}
private static long calculate_rabbit(long month) {
// TODO Auto-generated method stub
long cnt = 1;
long cnt_pre = 0;
long cnt_prepre = 0;
long i;
for(i = 1; i < month; i++)
{
cnt_prepre = cnt_pre;
cnt_pre = cnt;
cnt = cnt_pre+cnt_prepre;
}
return cnt;
}
}