回答
不知道你在運行這個(32位體系結構,64位?),但是你可能會溢出整數。你的答案會變得非常大。
即使'unsigned long long'也只能處理136. –
我真的不知道如果我在32位或64位上運行?有人告訴我,這個程序不能用C++來完成!!是不是正確?我的代碼是正確的,它只是結果太大而不適合,對嗎?重要的是,我對這個問題的理解是正確的,對嗎? – user3479130
在圖靈的意義上,我不會說「不能做」 - 有很多表示和處理非常大數字的技術,但是比本地類型足夠多的時候更多的工作。 – RobP
變量sum
溢出。甚至使它成爲一個unsigned long long
將最多隻能處理136使用double
爲sum
將讓你接近 - 481 - 但沒有在C++中提供的數據類型將處理所有一路攀升至500
如果你絕對必須去到500,你將需要使用一個可以支持大整數值的類。你不會說你使用的是什麼平臺和編譯器,但可以看看BigInteger class。
謝謝凱里,我還沒有學過類,我是一名新的計算機科學專業的學生 – user3479130
,它不一定是一個類,它可能只是一組函數,你可能會發現一些支持大整數的開源代碼,但是如果沒有,你必須自己編寫它。高級項目爲一個初級CS學生 –
非常感謝你Carey.I我想我使用無符號long long數據類型,因爲它提供了最多的結果:/。 – user3479130
unsigned long product(100);
for (unsigned long i(103); i<=500; i+=3)
{
product *= i;
std::cout << product << std::endl;
}
所以這個迭代步驟爲3,每個數字從100到500,計算產品然後輸出。
雖然RobP說這可能會溢出,因此我在上面的代碼中使用了無符號long。要了解有關檢測溢出看到這一點:
即使'unsigned long long'也遠不夠大。勉強過去100. –
我期望儘可能多,但不想做數學 – lachlan
謝謝你們,我開始認爲這超出了我的水平! – user3479130
我不知道這是否可以被接受,但這種打印產品(後未指定的所有格式是吧?)。
unsigned long long product = 1;
std::vector<int> fct;
for (int i = 100; i <= 500; i = i +3)
{
if ((i%2) == 0) {
int r = i/2;
fct.push_back(r);
std::cout << "product:" << product << " * 2";
std::vector<int>::iterator it = fct.begin();
while (it != fct.end()) {
std::cout << " * " << *it++;
}
std::cout << std::endl;
} else {
product = product * i;
std::cout << "product:" << product << " * 2";
std::vector<int>::iterator it = fct.begin();
while (it != fct.end()) {
std::cout << " * " << *it++;
}
std::cout << std::endl;
}
}
輸出:
產物:1 * 2 * 50
產品:103 * 2 * 50
產品:103 * 2 * 50 * 53
產品:11227 * 2 * 50 * 53
產品:11227 * 2 * 50 * 53 * 56
產物:1291105 * 2 * 50 * 53 * 56
產物:1291105 * 2 * 50 * 53 * 56 * 59
(...)
產物:2557801186831291169 * 2 * 50 * 53 * 56 * 59 * 62 * 65 * 68 * 71 * 74 * 77 * 80
產物:2557801186831291169 * 2 * 50 * 53 * 56 * 59 * 62 * 65 * 68 * 71 * 74 * 77 * 80 * 83
(...)
產物:9086141910361656411 * 2 * 50 * 53 * 56 * 59 * 62 * 65 * 68 * 71 * 74 * 77 * 80 * 83 * 86 * 89 * 92 * 95 * 98 * 101 * 104 * 107 * 110 * 113 * 116 * 119 * 122 * 125 * 128 * 131 * 134 * 137 * 140 * 143 * 146 * 149 * 152 * 155 * 158 * 161 * 164 * 167 * 170 * 173 * 176 * 179 * 182 * 185 * 188 * 191 * 194 * 197 * 200 * 203 * 206 * 209 * 212 * 215 * 218 * 221 * 224 * 227 * 230 * 233 * 236 * 239 * 242 * 245
產品:9086141910361656411 * 2 * 50 * 53 * 56 * 59 * 62 * 65 * 68 * 71 * 74 * 77 * 80 * 83 * 86 * 89 * 92 * 95 * 98 * 101 * 104 * 107 * 110 * 113 * 11 6 * 119 * 122 * 125 * 128 * 131 * 134 * 137 * 140 * 143 * 146 * 149 * 152 * 155 * 158 * 161 * 164 * 167 * 170 * 173 * 176 * 179 * 182 * 185 * 188 * 191 * 194 * 197 * 200 * 203 * 206 * 209 * 212 * 215 * 218 * 221 * 224 * 227 * 230 * 233 * 236 * 239 * 242 * 245 * 248
產品:14532515211626403169 * 2 * 50 * 53 * 56 * 59 * 62 * 65 * 68 * 71 * 74 * 77 * 80 * 83 * 86 * 89 * 92 * 95 * 98 * 101 * 104 * 107 * 110 * 113 * 116 * 119 * 122 * 125 * 128 * 131 * 134 * 137 * 140 * 143 * 146 * 149 * 152 * 155 * 158 * 161 * 164 * 167 * 170 * 173 * 176 * 179 * 182 * 185 * 188 * 191 * 194 * 197 * 200 * 203 * 206 * 209 * 212 * 215 * 218 * 221 * 224 * 227 * 230 * 233 * 236 * 239 * 242 * 245 * 248
謝謝lizusek。但我無法向我的導師介紹這一點。有一個我從未見過的術語。我會爲A +作弊:D。 – user3479130
@ user3479130是否需要這是真正的乘法?我不這麼認爲,因爲這不能被任何基本類型所代表。或者你真的誤解了這個任務。也許這只是打印每三個號碼? – 4pie0
什麼是「一個號碼的運行產品」?這是一個問題,問你的同事或教授......; ) – 4pie0
您使用整數數據類型的最大數量是, unsigned long long int b = 0XFFFFFFFFFFFFFFFF; (18446744073709551615)
在你的情況下,結果甚至比這個數字更大。
這是這個問題的轉折點。您可能必須找到處理大量數據的方法。我建議找到現有的API或算法。
我已經寫了一個程序來做到這一點...我沒有測試這個邊緣案例,可以有更好的方式來做到這一點。
現在,
#include <iostream>
#include <deque>
using namespace std;
void print_num(deque<int> &num) {
for(int i=0;i < num.size();i++) {
cout<<num[i];
}
cout<<endl;
}
deque<int> sum(deque<int> &oppA, deque<int> &oppB) {
if (oppA.size() == 0) return oppB;
if (oppB.size() == 0) return oppA;
deque<int> result;
unsigned int carry = 0;
deque<int>::reverse_iterator r_oppA = oppA.rbegin();
deque<int>::reverse_iterator r_oppB = oppB.rbegin();
while ((r_oppA != oppA.rend()) && (r_oppB != oppB.rend())) {
int tmp = *r_oppA + *r_oppB + carry;
result.push_front(tmp % 10);
carry = tmp/10;
r_oppB++;
r_oppA++;
}
while (r_oppA != oppA.rend()) {
int tmp = *r_oppA + carry;
result.push_front(tmp % 10);
carry = tmp/10;
r_oppA++;
}
while (r_oppB != oppB.rend()) {
int tmp = *r_oppB + carry;
result.push_front(tmp % 10);
carry = tmp/10;
r_oppB++;
}
return result;
}
deque<int> multiply(deque<int>& multiplicand, deque<int>& multiplier) {
unsigned int carry = 0;
deque<int> result;
int deci_cnt = 0;
deque<int>::reverse_iterator r_multiplier = multiplier.rbegin();
deque<int> tmp_result;
while (r_multiplier != multiplier.rend()) {
for (int i=0; i<deci_cnt ;i++) {
tmp_result.push_front(0);
}
deque<int>::reverse_iterator r_multiplicand = multiplicand.rbegin();
while (r_multiplicand != multiplicand.rend()) {
int tmp = (*r_multiplicand) * (*r_multiplier) + carry;
tmp_result.push_front(tmp % 10);
carry = tmp/10;
r_multiplicand++;
}
if (carry != 0) {
tmp_result.push_front(carry);
carry = 0;
}
result = sum(result, tmp_result);
deci_cnt++;
tmp_result.clear();
r_multiplier++;
}
return result;
}
deque<int> int_to_deque(unsigned long num) {
deque<int> result;
if (num == 0) {
result.push_front(0);
}
while (num > 0) {
result.push_front(num % 10);
num = num/10;
}
return result;
}
int main() {
deque<int> num1 = int_to_deque(18446744073709551615ULL);
deque<int> num2 = int_to_deque(18446744073709551615ULL);
deque<int> result = multiply(num1, num2);
print_num(result);
return 0;
}
輸出:340282366920928463426481119284349108225
- 1. 如何阻止我的批處理文件打印正在運行的代碼?
- 2. 在運行時檢測產品代碼或測試代碼是否在Swift中
- 3. 如何打印MUL產品的輸出?
- 4. magento產品的打印頁面
- 5. Woocommerce中產品陣列的打印值
- 6. 產品代碼流
- 7. 在運行代碼時遇到麻煩產品市場介紹
- 8. 捲髮器中的代碼正在按照laravel進行打印
- 9. 行代碼打印dublicates
- 10. Java代碼正在打印白頁
- 11. 代碼正在用逗號打印\ n
- 12. 正在尋找Instamapper的替代產品
- 13. 正在尋找w3c.dom.Node的替代產品
- 14. 產品在PHP MySQL的多個代碼
- 15. Javascript正在運行代碼
- 16. Python代碼打印不需要的行
- 17. 經過指定的時間運行python代碼後打印
- 18. 谷歌Apps腳本的HTML打印代碼,而不是運行
- 19. 如何從終端打印一行代碼的打印機?
- 20. 爲什麼我的代碼打印[I @ 87816d當我運行此代碼?
- 21. 訪問正在運行的Python代碼
- 22. 標識正在運行代碼的類
- 23. 數字產品,不打印數字
- 24. 如何從PHP打印產品標籤
- 25. 產品屬性插入打印/發票
- 26. 爲Sharepoint打印推薦產品
- 27. Array Foreach Loop打印前個產品
- 28. 的Android HTML代碼打印
- 29. 打印的分頁代碼
- 30. 代碼打印的票據
無論是增量我你的循環手動裏面,或者增加它在for循環。不要在兩個地方都這樣做,因爲它太混亂了。 –
謝謝尼爾。我做了你的建議,我得到了相同的輸出。 – user3479130
你有溢出,數字太大,無法將乘法結果放入任何類型 – 4pie0