兔子问题

题目:有一对兔子,每月生一对兔子,生下来的兔子过3个月后又能生一对兔子,问n个月后一共有多少只兔子。

代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 namespace Test1
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             int n ,i;
12             n=Convert.ToInt32(Console.ReadLine());
13             int[] a = new int[n];
14             if (n < 3)
15             {
16                  Console.WriteLine(1);
17                  Console.ReadLine();
18             }    
19             else
20             {
21                   a[0] = a[1] = 1;
22                   for (i = 2; i < n; i++)
23                       a[i] = a[i - 1] + a[i - 2];
24                   Console.WriteLine(a[n-1]);
25                   Console.ReadLine();
26             } 
27         }
28     }
29 }

 

posted @ 2017-09-29 13:16  淡淡的烦恼  阅读(236)  评论(0编辑  收藏  举报