static void Main(string[] args)
{
int[] appleNumOfBox = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
for (int boxNum = 0; boxNum < 10; boxNum++)
{
for (int applesToBuy = 1; applesToBuy < 1001; applesToBuy++)
{
if (!SumCanGet(appleNumOfBox, applesToBuy))
{
appleNumOfBox[boxNum] = applesToBuy;
break;
}
}
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine(appleNumOfBox[i].ToString()+"\t");
}
Console.ReadLine();
}
/// <summary>
/// 前面的加起来小于要买数量就返回false
/// </summary>
private static bool SumCanGet(int[] appleNumOfBox, int applesToBuy)
{
int sum = 0;
for (int i = 0; i < 10; i++)
{
sum += appleNumOfBox[i];
}
if (sum < applesToBuy)
return false;
else
return true;
}
这个题目很特殊,介于数列{2^(n-1)}前n项的和正好是2^n-1,小于第n+1项,而且可以证明数列{2^(n-1)}之和可以得到1~2^n-1之间的任何数,故有上解
运行结果1, 2, 4, 8, 16...,256,512
花生1
|
小虾三级
|园豆:872
|
2008-07-01 00:22