[英]How to show large integer numbers on C++
我正在開發一個計算C++ 上的 Fatorial Number 的代碼。
// Exemple: 5! = 5 x 4 x 3 x 2 x 1 = 120
#include <iostream>
using namespace std;
int main() {
int number, total;
cout << "Calculate fatorial number" << endl;
cout << "-------------------" << endl << endl;
cout << "Type a number... ";
cin >> number;
total = 1;
for (int i = number; i > 0; i-- ){
if (i == number){
total = i * total;
cout << number << "! = " << i << " x ";
} else if (i > 1) {
total = i * total;
cout << i << " x ";
} else {
total = i * total;
cout << i << " = ";
}
}
cout << total;
return 0;
}
當我給它數字時,不要按預期返回。
我想知道如何繞過更大的數字問題,所以我至少可以計算出 100!
您需要使用現有的處理大量數字的庫,或者實現自己的庫。 有很多選項,gnu 多精度,boost 等...
如果您選擇實現自己的,您會將數字存儲在以下內容中:
"90120304153543643626424262"
std::vector<int>
數字(以 10 為底) {9,0,1,2,0,....}
std::vector<int>
(大基數,為了提高效率。2^16 效果很好) {42567, 29183, 10987, ...}
然后,您需要滾動自己的乘法、加法和賦值。
你好,我解決大數的方法是這樣做
#include <iostream>
using namespace std;
typedef unsigned long long int bigint; //big int
int main() {
bigint total = 9494949494949497989;
cout<<total<<endl;
return 0;
}
所以我創建了一個名為 bigint 的自定義數據類型,然后我不做int total
我做bigint total
因此我能夠存儲大的 int 值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.