Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++中长整数数组的和_C++_Arrays_Sum Of Digits - Fatal编程技术网

C++中长整数数组的和

C++中长整数数组的和,c++,arrays,sum-of-digits,C++,Arrays,Sum Of Digits,我知道这个问题被问了很多次,但我在代码中遇到了不同的问题,我试图计算2-15之间的长整数之和 代码: 当我输入这三个数字时 1234567 123456 12345 输出: 总数为:1370368 但实际答案是:3703627 我尝试这些解决方案 但仍然没有得到正确的解决方案,如果用户输入不同范围的不同数字,我们如何解决这个问题。您的程序似乎假设所有数字都是7位数字: 1234567 123456[0] 12345[00]这不是关于编程,而是关于数学。。。 希望这有助于: 作为一个简单的示例,添

我知道这个问题被问了很多次,但我在代码中遇到了不同的问题,我试图计算2-15之间的长整数之和

代码:

当我输入这三个数字时

1234567 123456 12345 输出:

总数为:1370368

但实际答案是:3703627

我尝试这些解决方案
但仍然没有得到正确的解决方案,如果用户输入不同范围的不同数字,我们如何解决这个问题。

您的程序似乎假设所有数字都是7位数字:

1234567

123456[0]


12345[00]

这不是关于编程,而是关于数学。。。 希望这有助于:


作为一个简单的示例,添加1和11。结果如何?12或21?

我的代码用字符串对大数字求和。 首先,输入要累加到25的数字数。 然后输入180“每个数字”的数字

#include <iostream>
#include <stdlib.h>
using namespace std;
int main (){
    int band;
    cin >> band;
    string string_of_number[25];
    for (int i=0; i<band; i++){ //for get all string number
        cin >> string_of_number[i];
    }
    int strings_length[band];
    for (int i=0; i<band; i++){ //for get all length of strings
        strings_length[i]=string_of_number[i].length();
    }
    int answer[180];
    for(int i=0; i<180; i++){
        answer[i]=0;
    }
    int remaner=0;
    int sum=0;
    int last=180;
    for (int h=0; h<band; h++){ // for sum strings with sum answer one by one
        int j=179;
        for (int i=strings_length[h]; i>=0; i--){
        if(string_of_number[h][i]=='\0'){
            i--;
        }
        sum = (remaner + (string_of_number[h][i]-'0') + answer[j]) %10;
        remaner = ((string_of_number[h][i]-'0') + answer[j]) / 10;
        answer[j]=sum;
        if (i==0 && remaner>0){
            j--;
            answer[j]+=remaner;
            remaner=0;
        }
        if(last>j)
            last=j;
            j--;
           }
          }
        for(; last<180; last++){
        cout << answer[last];
        } 
   }

你的实际答案是3703627?根据,您的程序是正确的。您不需要第二个循环:只需执行sum+=array[i];在使用调试器的第一个循环中,输入25个数字,然后查看发生了什么:-@我在纸上计算出总数,得到了3703627。我真的不明白怎么会有人投赞成票。但答案是肯定的。我无法想象你的错误!我以为3703627是你的输出!
#include <iostream>
#include <stdlib.h>
using namespace std;
int main (){
    int band;
    cin >> band;
    string string_of_number[25];
    for (int i=0; i<band; i++){ //for get all string number
        cin >> string_of_number[i];
    }
    int strings_length[band];
    for (int i=0; i<band; i++){ //for get all length of strings
        strings_length[i]=string_of_number[i].length();
    }
    int answer[180];
    for(int i=0; i<180; i++){
        answer[i]=0;
    }
    int remaner=0;
    int sum=0;
    int last=180;
    for (int h=0; h<band; h++){ // for sum strings with sum answer one by one
        int j=179;
        for (int i=strings_length[h]; i>=0; i--){
        if(string_of_number[h][i]=='\0'){
            i--;
        }
        sum = (remaner + (string_of_number[h][i]-'0') + answer[j]) %10;
        remaner = ((string_of_number[h][i]-'0') + answer[j]) / 10;
        answer[j]=sum;
        if (i==0 && remaner>0){
            j--;
            answer[j]+=remaner;
            remaner=0;
        }
        if(last>j)
            last=j;
            j--;
           }
          }
        for(; last<180; last++){
        cout << answer[last];
        } 
   }