满意答案
zoav7
2015.10.15
采纳率:59% 等级:7
已帮助:962人
//第一种方法
int n = 100; //假设n=100
int[] Group = new int[1]; //初设牛的数量
Group[0] = 1;
for (int i = 1; i <= n; i++)
{//循环经过的 时间/年
int count = Group.Length;
for (int i2 = 0; i2 < count; i2++)
{ //循环所有的牛
if (Group[i2] == 4)
{
Array.Resize(ref Group, Group.Length + 1);
Group[Group.Length - 1] = 1;
}
else
{
Group[i2] += 1;
}
}
}
Console.Write("{0}年时有{1}头年", n, Group.Length);
Console.ReadKey();
//第二种方法
int n = 100; //假设n=100
int[] Group = new int[1]; //初设牛的数量
Group[0] = 1;
int i = 1;
while (i <= n)
{
int count = Group.Length;
int i2 = 0;
while (i2 < count)
{
if (Group[i2] == 4)
{
Array.Resize(ref Group, Group.Length + 1);
Group[Group.Length - 1] = 1;
}
else
{
Group[i2] += 1;
}
}
}
Console.Write("{0}年时有{1}头年", n, Group.Length);
Console.ReadKey();
00分享举报