2014-03-30 11 views
0

如何創建打印每三分之一的運行產品的C++代碼。打印正在運行的產品的代碼

我不知道我是否理解。

+0

無論是增量我你的循環手動裏面,或者增加它在for循環。不要在兩個地方都這樣做,因爲它太混亂了。 –

+0

謝謝尼爾。我做了你的建議,我得到了相同的輸出。 – user3479130

+0

你有溢出,數字太大,無法將乘法結果放入任何類型 – 4pie0

回答

0

不知道你在運行這個(32位體系結構,64位?),但是你可能會溢出整數。你的答案會變得非常大。

+0

即使'unsigned long long'也只能處理136. –

+0

我真的不知道如果我在32位或64位上運行?有人告訴我,這個程序不能用C++來完成!!是不是正確?我的代碼是正確的,它只是結果太大而不適合,對嗎?重要的是,我對這個問題的理解是正確的,對嗎? – user3479130

+0

在圖靈的意義上,我不會說「不能做」 - 有很多表示和處理非常大數字的技術,但是比本地類型足夠多的時候更多的工作。 – RobP

0

變量sum溢出。甚至使它成爲一個unsigned long long將最多隻能處理136使用doublesum將讓你接近 - 481 - 但沒有在C++中提供的數據類型將處理所有一路攀升至500

如果你絕對必須去到500,你將需要使用一個可以支持大整數值的類。你不會說你使用的是什麼平臺和編譯器,但可以看看BigInteger class

+0

謝謝凱里,我還沒有學過類,我是一名新的計算機科學專業的學生 – user3479130

+0

,它不一定是一個類,它可能只是一組函數,你可能會發現一些支持大整數的開源代碼,但是如果沒有,你必須自己編寫它。高級項目爲一個初級CS學生 –

+0

非常感謝你Carey.I我想我使用無符號long long數據類型,因爲它提供了最多的結果:/。 – user3479130

0
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。要了解有關檢測溢出看到這一點:

How to detect integer overflow?

+0

即使'unsigned long long'也遠不夠大。勉強過去100. –

+0

我期望儘可能多,但不想做數學 – lachlan

+0

謝謝你們,我開始認爲這超出了我的水平! – user3479130

0

我不知道這是否可以被接受,但這種打印產品(後未指定的所有格式是吧?)。

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

+0

謝謝lizusek。但我無法向我的導師介紹這一點。有一個我從未見過的術語。我會爲A +作弊:D。 – user3479130

+0

@ user3479130是否需要這是真正的乘法?我不這麼認爲,因爲這不能被任何基本類型所代表。或者你真的誤解了這個任務。也許這只是打印每三個號碼? – 4pie0

+0

什麼是「一個號碼的運行產品」?這是一個問題,問你的同事或教授......; ) – 4pie0

0

您使用整數數據類型的最大數量是, 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