poj 2756 Autumn is a Genius

题目大意:

输入两个整数a,b<32768(没说大于0,就有可能是负无穷,所以是一道高精度),求a+b

a和b的前面都有可能'+'或者'-'

Sample Input

4
1 2
+1 +2
+1 -2
-1 -2

Sample Output

3
3
-1
-3

 

题目解答:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>

using namespace std;

int n;
void update(string &s){
    int i;
    for(i = 0; i < s.size() - 1; i++){
        if(s[i] != '0'){
            break;
        }
    }
    s.erase(s.begin(),s.begin()+i);
}

int compare(string &s1,string &s2){
    int biao = 1;
    if(s1.size() < s2.size())  biao = 0;
    else if(s1.size() == s2.size()){
        for(int i = 0; i < s1.size();i++){
            if(s1[i] == s2[i])  continue;
            else if(s1[i] > s2[i]){
                break;
            }
            else{
                biao = 0;
                break;
            }
        }
    }
    if(biao == 0){
        string tmp = s1;
        s1 = s2;
        s2 = tmp;
    }
    return biao;
}
string sub(string s1,string s2){
    int tuiwei = 0;
    for(int i = s2.size() - 1,j = s1.size() - 1; j >= 0; i--,j--){
        if(i >= 0){
            if(s2[i] <= s1[j] - tuiwei){
                s1[j] = '0' + s1[j] - s2[i] - tuiwei;
                tuiwei = 0;
            }
            else{
                s1[j] = '0' + s1[j] + 10 - s2[i] - tuiwei;
                tuiwei = 1;
            }
        }
        else{
            if('0' <= s1[j] - tuiwei){
                s1[j] = s1[j] - tuiwei;
                tuiwei = 0;
            }
            else{
                s1[j] = s1[j] + 10 - tuiwei;
                tuiwei = 1;
            }
        }
    }
    update(s1);
    return s1;
}
string add(string s1,string s2){
    int jinwei=0;
    int lens1 = s1.size();
    int lens2 = s2.size();
    if(lens1 < lens2){
        string tmp = s1;
        s1 = s2;
        s2 = tmp;
    }
    for(int i = s2.size() - 1,j = s1.size() -1; j >= 0; i--,j--){
        if(i >= 0){
            int he = (s1[j] - '0') + s2[i] - '0' + jinwei;
            if(he >= 10){
                jinwei = 1;
                s1[j] = '0' + he - 10;
            }
            else{
                jinwei = 0;
                s1[j] = '0' + he;
            }
        }
        else{
            int he = (s1[j] - '0') + jinwei;
            if(he >= 10){
                jinwei = 1;
                s1[j] = '0' + he - 10;
            }
            else{
                jinwei = 0;
                s1[j] = '0' + he;
            }
        }
    }
    if(jinwei == 1)  s1 = "1"+s1;
    return s1;
}
char str1[1111000],str2[1111000];
int main(){
    scanf("%d", &n);
    while(n--){
        scanf("%s%s", str1, str2);
        string s1 = str1;
        string s2 = str2;
        if(s1[0] == '+'){
            s1.erase(s1.begin());
        }
        if(s2[0] == '+'){
            s2.erase(s2.begin());
        }
        if(s1[0] == '-' && s2[0] == '-'){
            s1.erase(s1.begin());
            s2.erase(s2.begin());
            update(s1);
            update(s2);
            printf("-%s\n", add(s1,s2).c_str());
        }
        else if(s1[0] == '-'){
            s1.erase(s1.begin());
            update(s1);
            update(s2);
            int fu = compare(s2,s1);
            if(fu == 0)  printf("-%s\n", sub(s2,s1).c_str());
            else printf("%s\n", sub(s2,s1).c_str());
        }
        else if(s2[0] == '-'){
            s2.erase(s2.begin());
            update(s1);
            update(s2);
            int fu = compare(s1,s2);
            if(fu == 0)  printf("-%s\n", sub(s1,s2).c_str());
            else printf("%s\n", sub(s1,s2).c_str());
        }
        else {
            update(s1);
            update(s2);
            printf("%s\n", add(s1,s2).c_str());
        }
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值