有一对幼兔,幼兔经过一个月长为小兔,小兔经过一个月长为大兔并且生下一对幼兔,而且大兔每月还会生下一对幼兔,问N个月后有多少对兔子
Console.WriteLine("请输入经过了几个月:"); int n = Convert.ToInt32(Console.ReadLine()); int syt = 1, byt = 0; int sxt = 0, bxt = 0; int sdt = 0, bdt = 0; for (int i = 1; i <= n; i++) { byt = sdt + sxt; bxt = syt; bdt = sdt + sxt; syt = byt; sxt = bxt; sdt = bdt; } Console.WriteLine("经过" + n + "个月后:"); Console.WriteLine("幼兔为:" + byt + "对"); Console.WriteLine("小兔为:" + bxt + "对"); Console.WriteLine("大兔为:" + bdt + "对"); int sum = byt + bxt + bdt; Console.WriteLine("兔子总共有:" + sum + "对"); Console.WriteLine("兔子总共有:{0}对,小兔子有{1}对", sum, bxt);
时间: 2024-10-10 06:54:55