【问题标题】:Debug C++ code (add two very big numbers)调试 C++ 代码(添加两个非常大的数字)
【发布时间】:2022-01-11 14:07:53
【问题描述】:

这段代码应该得到两个非常大的数字并将它们相加。只允许 iostream 和字符串库。

示例输入:

132163162986831298132869132968213689316298613298681329689312682136312621382931628613286831286921968312698312698132682136893162986832168312698132
8312961362983126893162986312986832196893126813268932169831268912869621386893126893126891326831268361298621398631286831269813268312698132689312683612986892136813268312698312698321686312986312986893216831268921368321698132689312698132689321683126893216986893216893126813268931286931629886312

示例输出:

8312961362983126893162986312986832196893126813268932169831268912869621386893126893126891326831268361298621398631286831269813268312698132689312683745150055123644566445567445666535375629284926285574546520581603504634319515620941311419520608605095205915299591349575263706431918119099942584444
#include <iostream>
#include <string>

using namespace std;

int main (){
    string num1 , num2 ,sum;
    getline(cin,num1);
    getline(cin,num2);
    if (num2.length() > num1.length())
    {
        num2.swap(num1);
    }
    std::reverse(num1.begin(), num1.end());
    std::reverse(num2.begin(), num2.end());
    while(num2.length() < num1.length())
        num2.push_back('0');
    size_t lnth = num1.length();
    unsigned tmp , holder=0;
    for (size_t i = 0;i < lnth ; i++){
        tmp = (num1[i] - '0') + (num2[i] - '0') + holder;
        sum.push_back(tmp % 10 + '0');
        holder = tmp / 10;
    }
    if(holder > 0){
        sum.push_back('0' + holder);
    }
    while(!sum.empty()){
        if (sum[sum.length() - 1] == '0'){
            sum.pop_back();
        }
        else{
            break;
        }
    }
    std::reverse(sum.begin(), sum.end());
    cout << sum;
}

我得到了一些正确和一些错误的答案;

【问题讨论】:

  • 你得到什么错误?

标签: c++ arrays string


【解决方案1】:

首先,给定size_t i,根据定义,条件i &gt;= 0 始终为真。

这是因为size_t 产生了一个 2s 补码的无符号表示。

因此,循环 for (size_t i = whatever; i &gt;= 0; i--) 将永远运行,或者直到“发生不好的事情”。

【讨论】:

    【解决方案2】:

    你知道std::string 有一个很棒的功能叫做“反向迭代器”吗? 它让你的生活变得更轻松:

    auto it1 = str1.crbegin(), it2 = str2.crbegin();
    int carry = 0;
    std::string result;
    while (it1 != str1.crend() && it2 != str2.crend()) {
      int sum = (*it1 - '0') + (*it2 - '0') + carry;
      result.push_back('0' + (sum % 10));
      carry = sum / 10;
      it1++; it2++;
    }
    
    // We assume str1 is the longest, so copy digits from str1
    while (it1 != str1.crend()) {
      int sum = (*it1 - '0') + carry;
      result.push_back('0' + (sum % 10));
      carry = sum / 10;
      it1++;
    }
    
    // This is an edge case: if str1.length == str2.length, carry might be 1
    if (carry) {
      result.push_back('1');
    }
    
    // Reverse result
    result = std::string(result.crbegin(), result.crend());
    

    【讨论】:

    • 我使用了你的想法(不是复制),但得到了一些错误的答案。你能看看吗?
    • 我没有发现任何明显的错误。用一些输入和错误的输出编辑你的问题
    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    相关资源
    最近更新 更多