有一对兔子,从出生后的第3个月起每个月都生一对兔子。小兔子长到第3个月后每个月又生一对兔子,假设所有的兔子都不死,问30个月后的兔子总数为多少。
1.定义3个长整形long fib1 = 1,fib2 = 1,fib;
2.建立循环 令fib = fib1 + fib2;fib即为当月的兔子数
3.令fib2 = fib1,fib1 = fib 准备下一次循环。
#include<iostream>
using namespace std;
int main()
{
long fb1 = 1, fb2 = 1, fb;
cout << fb1 << " " << fb2<<endl;
for (int i = 3; i <= 30; i++) {
fb = fb1 + fb2;
cout << fb<<" ";
if (i % 4 == 0) {
cout << endl;
}
fb2 = fb1;
fb1 = fb;
}
return 0;
}