大数加法

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
int main()
{
    string str1;
    string str2;
    int jw=0;
    int arr[10005];
    int l=0;
    int i;
    int num;
    cin>>str1;
    cin>>str2;
    reverse(str1.begin(),str1.end());
    reverse(str2.begin(),str2.end());
    int len = str1.length()>str2.length()?str2.length():str1.length();
    for(i=0;i<len;i++){
        num = str1[i]-'0'+str2[i]-'0';
        arr[l++] = num;
    }
    if(len==str1.length()){
        for(i=len;i<str2.length();i++){
            arr[l++] = str2[i]-'0';
        }
    }else{
        for(i=len;i<str1.length();i++){
            arr[l++] = str1[i]-'0';
        }
    }
    for(i=0;i<l;i++){
        arr[i] = arr[i]+jw;
        jw=0;
        while(arr[i]>=10){
            jw += arr[i]/10;
            arr[i] = arr[i]%10;
        }
    }
    while(jw!=0){
        arr[l++] = jw%10;
        jw = jw/10;
    }
    for(i=l-1;i>=0;i--){
        printf("%d",arr[i]);
    }
    return 0;
} 

 

posted @ 2018-10-13 23:36  菜の可怜  阅读(118)  评论(0编辑  收藏  举报