C#语言设计 兔子繁殖问题。

2025-02-23 22:38:14
推荐回答(4个)
回答1:

设F(n)为第n个月兔子对数,F(1)=1,F(2)=1,F(3)=2,F(4)=3,F(5)=5......
F(n)=F(n-2)+F(n-1)
典型的求斐波那契数列问题。
using System;

namespace Rabbit
{
public class Rabbit
{

public static void Main()
{
int i=4,fn_2=1,fn_1=2;
int fn;
while(i<=20)
{
fn=fn_2+fn_1;
fn_2=fn_1;
fn_1=fn;
i++;
}
Console.Write(fn);
Console.ReadKey();

}
}

}

回答2:

int month = 0;
int x = 1;
int sum = 1;
Console.WriteLine("请输入月份");
month = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i < month; i++)
{
x = sum - x;
sum += x;
}
Console.WriteLine("第{0}月兔子的对数为{1}对", month, sum);
Console.ReadKey();

回答3:

参照 阿涛养殖场 的理论
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testProject
{
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("please enter the month .");
int month = int.Parse(Console.ReadLine());
long sum = calculator(month);
Console.WriteLine(string.Format("the result is {0} .",sum.ToString()));
}

}
public static long calculator(int month)
{
if (month == 1 || month == 2)
{
return 1;
}
else
{
return calculator(month - 1) + calculator(month - 2);
}
}
}
}

回答4:

朋友,程序我不会呀。但要是给你计算说明没问题