Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit223cfdf

Browse files
authored
Update 兔子生兔子的问题.c
1 parentc0e318c commit223cfdf

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed
Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,47 @@
1-
#
1+
//有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
2+
3+
//兔子的规律为数列:1, 1, 2, 3, 5, 8, 13, 21 ....
4+
5+
//下面使用了迭代解法。
6+
7+
#include<stdio.h>
8+
intmain()
9+
{
10+
longf1=1,f2=1;// 兔子的数量
11+
inti;// 循环次数
12+
intn;// 要计算的月份
13+
14+
printf("输入要计算的月数:");
15+
scanf("%d",&n);
16+
17+
// 计算出循环次数
18+
if(n%2==1){
19+
n= (n+1)/2;
20+
}else{
21+
n=n/2;
22+
}
23+
24+
for(i=1;i<=n;i++){
25+
printf("第%d个月有%d只\n",i*2-1,f1);
26+
printf("第%d个月有%d只\n",i*2,f2);
27+
28+
f1=f1+f2;/*前两个月加起来赋值给第三个月*/
29+
f2=f1+f2;/*前两个月加起来赋值给第三个月*/
30+
}
31+
return0;
32+
}
33+
34+
35+
//运行结果:
36+
//运行结果:
37+
//输入要计算的月数:10
38+
//第1个月有1只
39+
//第2个月有1只
40+
//第3个月有2只
41+
//第4个月有3只
42+
//第5个月有5只
43+
//第6个月有8只
44+
//第7个月有13只
45+
//第8个月有21只
46+
//第9个月有34只
47+
//第10个月有55只

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp