[問題] 找perfect number

看板C_and_CPP作者 (工人)時間16年前 (2009/11/06 17:19), 編輯推噓0(007)
留言7則, 4人參與, 最新討論串1/1
開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) VC++ 有問題的code: (請善用置底文標色功能) #include "stdafx.h" #include"iostream"//標頭檔 #include"iomanip" #include"cmath" using namespace std; using std::setw; using std::fixed; using std::setprecision; void PerfectNumber(unsigned long double num,unsigned long double &count); int _tmain(int argc, _TCHAR* argv[]) { unsigned long double a,i,j,k,co=0; cin>>a; for(i=2;i<=a;i++) { PerfectNumber(i,co); } cout<<"the total is "<<co<<endl; system("pause"); return 0; } void PerfectNumber(unsigned long double num,unsigned long double &count) { unsigned long double i,j,sum=0; //count=0; if(num%2==0) { for(j=1;j<num-1;j++) if(num%j==0) { sum=sum+j; } if(sum==num) { cout<<num<<" is a perfect number"<<endl; count=count+1; } } } 補充說明: 以編譯過但不知要怎找才能找到8589069059 就是可以讓他快一點嗎 以上CODE是過的 謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.7.59

11/06 17:20, , 1F
我要宣告何種變數才可
11/06 17:20, 1F

11/06 17:29, , 2F
8589069059 > 2147483647
11/06 17:29, 2F

11/06 18:05, , 3F
所以有別種宣告方式嗎?
11/06 18:05, 3F

11/06 18:27, , 4F
long long ?
11/06 18:27, 4F

11/06 19:11, , 5F
google for GMP
11/06 19:11, 5F

11/07 13:03, , 6F
其實我猜double就可以了 浮點數在處理有效數字容量內的
11/07 13:03, 6F

11/07 13:04, , 7F
大整數時並不像處理小整數或實數會有不精確性
11/07 13:04, 7F
文章代碼(AID): #1Ay-eb0K (C_and_CPP)