大数加法 C++ 竖式模拟 高精度

高精度

1005 大数加法

  1. 1.0 秒
  2.  
  3. 131,072.0 KB
  4.  
  5. 0 分
  6.  
  7. 基础题

给出2个大整数A,B,计算A+B的结果。

 收起

输入

第1行:大数A
第2行:大数B
(A,B的长度 <= 10000 需注意:A B有可能为负数)

输出

输出A + B

输入样例

68932147586
468711654886

输出样例

537643802472

 

#include<bits/stdc++.h>
using namespace std;

#define clr(a) memset(a, 0, sizeof(a))
#define line cout << "--------" << endl


typedef long long ll;
const int maxn = 1e5 + 10;


char a[maxn], b[maxn], ta[maxn], tb[maxn];
int na[maxn], nb[maxn], ans[maxn], cnt = 0;
bool flaga = true, flagb = true;

int cmp(char sa[], char sb[]){
    int lsa = strlen(sa);
    int lsb = strlen(sb);
    if(strcmp(sa, sb) == 0) return 0;
    if(lsa > lsb) return 1;
    if(lsa < lsb) return -1;
    if(lsa == lsb){
        for(int i = lsa; i >= 0; i--){
            if(sa[i] > sb[i]) return 1;
            else if(sa[i] < sb[i]) return -1;
            else continue;
        }
    }
}

int main(){

    gets(a);
    gets(b);
    int la = strlen(a);
    int lb = strlen(b);
    int tt = 0;

    if(a[0] == '-'){
        tt ++;
        flaga = false;
        for(int i = 1; i < la; i++)
            ta[i-1] = a[i];
        la --;
    }
    else{
        for(int i = 0; i < la; i++)
            ta[i] = a[i];
    }
    reverse(ta, ta+la);

    if(b[0] == '-'){
        tt ++;
        flagb = false;
        for(int i = 1; i < lb; i++)
            tb[i-1] = b[i];
        lb --;
    }
    else {
        for(int i = 0; i < lb; i++)
            tb[i] = b[i];
    }
    reverse(tb, tb+lb);

    for(int i = 0; i < la; i++)
        na[i] = ta[i] - '0';
    for(int i = 0; i < lb; i++)
        nb[i] = tb[i] - '0';
    if(tt % 2 == 0){
        int len = max(la, lb);
        int temp = 0;
        for(int i = 0; i < len; i++){
            int x = na[i] + nb[i] + temp;
            if(x > 9){
                ans[cnt++] = x % 10;
                temp = 1;
            }
            else{
                ans[cnt++] = x;
                temp = 0;
            }
        }
        if(temp) ans[cnt++] = 1;
        reverse(ans, ans+cnt);
        if(!flaga) cout << "-";
        for(int i = 0; i < cnt; i++){
            cout << ans[i];
        }
        cout << endl;
    }
    else{
        if(flaga){
            int x = cmp(ta, tb);
            if(x == 0){
                cnt = 1;
                ans[0] = 0;
            }
            else if(x > 0){
                int temp = 0;
                for(int i = 0; i < la; i++){
                    int k = na[i];
                    if(temp) k--;
                    k -= nb[i];
                    if(k < 0){
                        temp = 1;
                        k += 10;
                    }
                    else temp = 0;
                    ans[cnt++] = k;
                }
 
                int pos = cnt-1;
                for(int i = cnt-1; i >= 0; i--){
                    if(ans[i] == 0) pos--;
                    else break;
                }
                for(int i = pos; i >= 0; i--)
                    cout << ans[i];
                cout << endl;
            }
            else{
                int temp = 0;
                for(int i = 0; i < lb; i++){
                    int k = nb[i];
                    if(temp) k--;
                    k -= na[i];
                    if(k < 0){
                        temp = 1;
                        k += 10;
                    }
                    else temp = 0;
                    ans[cnt++] = k;
                }
                cout << "-";
                int pos = cnt-1;
                for(int i = cnt-1; i >= 0; i--){
                    if(ans[i] == 0) pos--;
                    else break;
                }
                for(int i = pos; i >= 0; i--)
                    cout << ans[i];
                cout << endl;

            }
        }
        else{//b + a -
            int x = cmp(ta, tb);
            if(x == 0){
                cnt = 1;
                ans[0] = 0;
            }
            else if(x > 0){
                int temp = 0;
                for(int i = 0; i < la; i++){
                    int k = na[i];
                    if(temp) k--;
                    k -= nb[i];
                    if(k < 0){
                        temp = 1;
                        k += 10;
                    }
                    else temp = 0;
                    ans[cnt++] = k;
                }
                cout << "-";
                int pos = cnt-1;
                for(int i = cnt-1; i >= 0; i--){
                    if(ans[i] == 0) pos--;
                    else break;
                }
                for(int i = pos; i >= 0; i--)
                    cout << ans[i];
                cout << endl;
            }
            else{
                int temp = 0;
                for(int i = 0; i < lb; i++){
                    int k = nb[i];
                    if(temp) k--;
                    k -= na[i];
                    if(k < 0){
                        temp = 1;
                        k += 10;
                    }
                    else temp = 0;
                    ans[cnt++] = k;
                }
                int pos = cnt-1;
                for(int i = cnt-1; i >= 0; i--){
                    if(ans[i] == 0) pos--;
                    else break;
                }
                for(int i = pos; i >= 0; i--)
                    cout << ans[i];
                cout << endl;
            }
        }
    }
    return 0;
}

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页