作者tom21tom21 (平凡就好)
站內Programming
標題[問題] c++ 數字很大
時間Sat Apr 16 20:41:14 2011
double force_method(item *&list , const int &number , const double &weight){
double maxvalue=0 ; //暴力法的最佳解
long long int maxnumeber;
int *XB;
long long int temp1 = ( long long int ) pow ( 2 , number );
for( long long int i = 1 ; i < temp1 ; i++){//1~n^2-1
int *x;
x = new int [ number ];
long long int temp=i;
double sofarweight=0 , sofarvalue=0 ;//紀錄目前重量 和價值
for ( int j=0 ; j < number ; j++ ){
//十進位轉成二進位
x[ j ] = temp % 2 ;
temp = temp / 2 ;
//計算重量
if( x [ j ] == 1 ){
sofarweight += list[ j ].weight;
sofarvalue += list[ j ].value;
}
}
if( sofarweight <= weight && sofarvalue > maxvalue ){
maxvalue = sofarvalue;
maxnumeber = i;
XB=x;
}
}
return maxvalue;
}
我的問題在於 當number大於25的時候 程式就無法回傳值
其餘的沒有問題(值得傳輸 演算法)
第一次遇到沒有經驗 請大大多多指教
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.47.20.115
→ Dannvix:做大數 or 換語言 125.227.87.109 04/16 21:04
→ tom21tom21:請問一下 做大數是什麼意思 我測試過 114.47.20.115 04/16 21:12
→ tom21tom21:long long int是32bit 足夠用 114.47.20.115 04/16 21:13
推 suhorng:long long是64-bit才對吧.... 61.217.32.189 04/16 21:33
→ suhorng:大數是指自己寫程式模擬直式做加減乘除 61.217.32.189 04/16 21:33
→ firejox:要做2的次方可以用左移去做123.240.128.241 04/16 23:34