当前位置:网站首页>Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set

Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set

2022-07-06 14:42:00 No two or three things

experiment 4-1-10 The problem of rabbit reproduction

fraction 15

Full screen browsing topics

Switch layout

author Xu Jingchun

Company Zhejiang University

A couple of Rabbits , From the day after birth 3 A couple of rabbits are born every month from . The little rabbit grows to the third place 3 Two months later, a couple of rabbits were born every month . If the rabbits don't die , May I ask 1 A pair of rabbits born six months ago , It will take at least a few months for the total number of rabbits to reach N Yes ?

Input format :

The input gives no more than 10000 The positive integer N.

Output format :

Output the total number of rabbits in one line to N Minimum number of months required .

sample input :

30

sample output :

9

Code length limit

16 KB

The time limit

400 ms

Memory limit

64 MB

Code implementation :

# include <stdio.h>
main() {
	int N;
	scanf("%d",&N);// 1,1,2,3 Satisfy Fibonacci sequence 
	int a = 1,b = 1,max = 1,temp,month = 1;
	if (N == 1) {
		printf("%d",month);
	}else {
		month = 2;
		while(max < N) {
			temp = a;
			a += b;
			b = temp;
			max = (a >= b) ? a : b;
			month += 1;
		}
		printf("%d",month);
	}
	return 0;
}

原网站

版权声明
本文为[No two or three things]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060918312183.html