将int型改为 long int型也就可以计算15堆,改为unsign long int才可以计算31堆啊!我知道错在那里了.
请问有什么方法可以保存更大的整数呢???
"32楼算20堆 81毫秒,不过算更多也不行"请问怎样计算时间的?????

Finding!!!
最省脑子的方法,把所有可能的堆法列一下,直接数。
#include "math.h"
int main(int argc, char* argv[])
{
long lTryTimes, lCount, lCurrentCount;
int intStoneNumber, intStoneWeight[20], intTotalWeight=0, intTmp, intCurrentWeight, intMinDiff;
scanf("%d", &intStoneNumber);
for (intTmp = 0; intTmp < intStoneNumber; intTmp++)
{
scanf("%d", &intStoneWeight[intTmp]);
intTotalWeight += intStoneWeight[intTmp];
}
lTryTimes = (int)pow(2, intStoneNumber);
intMinDiff = intTotalWeight;
for (lCount = 0; lCount < lTryTimes; lCount++)
{
lCurrentCount = lCount, intCurrentWeight = 0;
for (intTmp = 0; intTmp < intStoneNumber; intTmp++)
{
if (lCurrentCount % 2)
intCurrentWeight += intStoneWeight[intTmp];
lCurrentCount /= 2;
}
if (intMinDiff > abs(intTotalWeight - intCurrentWeight * 2))
intMinDiff = abs(intTotalWeight - intCurrentWeight * 2);
}
printf("min diff is %d\n", intMinDiff);
return 0;
}
long start ,end;
----
----
start=clock();
----
---
end=clock();
end-start 就是运行时间(单位:毫秒)
long start ,end;
----
----
start=clock();
----
---
end=clock();
end-start 就是运行时间(单位:毫秒)
谢谢!!
你说用32楼的方法计算20堆用了81毫秒,大于20堆又不行了,干嘛不行了??
这组数据我测试了,用时0豪秒~ 我怀疑你用的是286
这题用时和数字多少是呈几何级数的,和数字大小可以说没关系
32楼的我的意思是算20堆以后的越来越慢,数字越大越吃力
例如石头有30堆,用了91秒才算出来,累
你的不能算14以上的,可能是算的时间太长,马上出不了结果,所以你以为算不了?
这组数据我测试了,用时0豪秒~ 我怀疑你用的是286
这题用时和数字多少是呈几何级数的,和数字大小可以说没关系
32楼的我的意思是算20堆以后的越来越慢,数字越大越吃力
例如石头有30堆,用了91秒才算出来,累
你的不能算14以上的,可能是算的时间太长,马上出不了结果,所以你以为算不了?
看一下,这就是用你的程序在我的电脑上运行的情况:time is:696 910 768.
我是没骗你的!也许你电脑是快,但在我的电脑 上出问题起码可以说明程序的通用性!!你也不要小看人,我这台电脑据说是奔4的.
还有你看到了没有,我在最后输入12个千位数字,计算他们用了1022毫秒!!!更惨是输出的结果是-24492.是负值!!!
[此贴子已经被作者于2006-5-15 8:10:36编辑过]
哎!算了,都是我这个人太马虎!!我算出来的时间包含了我输入数字的时间!!!
现在我改了,发现程序运行确实用还不到1毫秒,我是用%d输出时间的,所以输出0.
但有一点没错,就是你程序的运行结果还是-24492,错的!!!
正确的答案是5868.
你可以用下面的方法检验一下:0 1 0 0 0 0 0 0 0 0 0 0
就是用23456减去其他.
我的测试数据是:1234 23456 1245 1263 1475 1596 1236 2365 2321 2145 1256 1452