1065 A+B and C (64bit) (20 分) 两种做法(高精度数的运算以及溢出)样例二存在问题

首先说一下样例二:
两个负数相加判断大小。
对于第一种方法(高精度运算):
比较函数出现了错误;用的是字符串比较,应该去掉符号填充0进行比较,而不可以直接填充0
对于第二种方法:
char == 1 Byte;
short int == 2 Byte;
int == 2 Byte;
unsigned int == 2 Byte;
long == 4 Byte;
unsigned long == 4 Byte;
long long == 8 Byte;
float == 4 Byte;
double == 8 Byte.
对于溢出;
八个字节的数位,代表的范围是-128-127
正数位的溢出不可能为0
负数位的溢出可能为0
所以在第二ci判断的时候要把tem=0加上(重点)

代码如下:
(两种方法)

#include<bits/stdc++.h>
using namespace std;
int ai[1000],bi[1000],ci[1000];
string addhigh(string a , string b){
    //先把高精度字符串转化为int数组
    int count = 0;
    for(int i = a.size()-1 ; i >= 0 ; i--){
        ai[count++] = a[i]-'0';
    }
    count = 0;
    for(int i = b.size()-1 ; i >= 0 ; i--){
        bi[count++] = b[i]-'0';
    }
    //进行高精度加法的相加
    int carry = 0;
    int length = max(a.size(),b.size());
    count = 0;
    for(int i = 0 ; i < length ; i++){
        ci[count++] = (ai[i]+bi[i]+carry)%10;
        carry = (ai[i]+bi[i]+carry)/10;
    }
    if(carry != 0)
        ci[count] = carry;
    int index = 0;
    for(int i = length ; i>=0 ; i--){
        if(ci[i] != 0){
            index = i;
            break;
        }
    }
    string result = "";
    for(int i = index ; i>=0 ; i--){
        result = result+to_string(ci[i]);
    }
    return result;
}
int compareString(string a, string b){
    int flag = 0;
    string ac(a);
    string bc(b);
    if(a[0]=='-'&&b[0]!='-')
        return -1;
    else if(a[0]!='-'&&b[0]=='-')
        return 1;
    else if(a[0]=='-'&&b[0]=='-'){
        flag = 1;
        ac.erase(ac.begin());
        bc.erase(bc.begin());
    }
    int length = max(a.size(),b.size());
    while(ac.size() != length){
        ac = "0"+ac;
    }
    while(bc.size() != length){
        bc = "0"+bc;
    }
    if(flag == 1)
        return strcmp(bc.c_str(),ac.c_str());
    else
        return strcmp(ac.c_str(),bc.c_str());
}

string highreduce(string a , string b){//里面传的参数默认是两个正数
    int flag = 0;//判断最后是否需要添加减号
    string ac(a);
    string bc(b);
    if(compareString(ac,bc) < 0){
        flag = 1;
        swap(ac,bc);
    }
    //进行字符串的转换
    int count = 0;
    for(int i = ac.size()-1 ; i >= 0 ; i--){
        ai[count++] = ac[i]-'0';
    }
    count = 0;
    for(int j = bc.size()-1 ; j >= 0 ; j--){
        bi[count++] = bc[j]-'0';
    }
    
    //进行数位的相减
    int length = max(ac.size(),bc.size());
    for(int i = 0 ; i < length ; i++){
        if(ai[i] < bi[i]){
            ci[i] = (ai[i]+10)-bi[i];
            ai[i+1]--;
        }else{
            ci[i] = ai[i]-bi[i];
        }
    }
    int index = 0;
    for(int i = length ; i>=0 ; i--){
        if(ci[i] != 0){
            index = i;
            break;
        }
    }
    string result = "";
    for(int i = index ; i>=0 ; i--){
        result = result+to_string(ci[i]);
    }
    if(flag == 1)
        result = "-"+result;
    return result;
}


int main(){
    int n;
    cin>>n;
    int count = 1;
    while(n--){
        fill(ai,ai+1000,0);
        fill(bi,bi+1000,0);
        fill(ci,ci+1000,0);
        string a,b,c;
        cin>>a>>b>>c;
        string result = "";
        if(a[0] == '-'&&b[0]=='-'){
            result = addhigh(a.substr(1,a.size()-1),b.substr(1,b.size()-1));
            result = '-'+result;
        }else if(a[0] == '-'&&b[0]!='-'){
            result = highreduce(b,a.substr(1,a.size()-1));
        }else if(a[0] != '-'&&b[0] =='-'){
            result = highreduce(a,b.substr(1,b.size()-1));
        }else{
            result = addhigh(a,b);
        }
//          cout<<result;
        if(compareString(result,c)>0){            
            cout<<"Case #"<<count<<": true"<<endl;
            count++;
        }
        else{            
            cout<<"Case #"<<count<<": false"<<endl;
            count++;
        }
    }
}

第二种方法:

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long a,b,c;
    int n;
    cin>>n;
    int count = 1;
    while(n--){
        scanf("%lld %lld %lld",&a,&b,&c);
        long long tem = a+b;
        if(a>0&&b>0&&tem<0){
            printf("Case #%d: true\n",count);
        }else if(a<0&&b<0&&tem>=0)
            printf("Case #%d: false\n",count);
        else{
            if(tem>c){
                printf("Case #%d: true\n",count);
            }else{
                printf("Case #%d: false\n",count);
            }
        }    
        count++;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值