兔子繁殖问题(斐波那契数列)
做题记录代码。
c++:
#include<iostream>
#include<cstdio>
using namespace std;
int main()//兔子繁殖问题,斐波那契数列。an=an-1+an-2
{
int a = 0, b = 1, N,m=1,month=1;
cin >> N;
while (m<N)
{
m = a + b;//第三个月的;
a = b;
b = m;
month++;
}
cout << month;
return 0;
}
c#
using System;
namespace pta2._6
{
class Program
{
static void Main(string[] args)
{
int N, mon = 1, m = 1, n = 0, num = 1;
N = Convert.ToInt32(Console.ReadLine());
while (num < N)
{
num = m + n;
n = m;
m = num;
mon++;
}
Console.WriteLine("{0}", mon);
}
}
}