pta A1065 A+B and C (64bit) (20分) 大数求和比大小

https://pintia.cn/problem-sets/994805342720868352/problems/994805406352654336

Given three integers A, B and C in [−2​^63​​,2^​63​​], you are supposed to tell whether A+B>C
我的测试数据如下:
8 -9223372036854775807 -9223372036854775808 3 9223372036854775807 9223372036854775808 -100000000000000 -9223372036854775807 -9223372036854775808 -3 9223372036854775807 9223372036854775808 3 -9223372036854775807 9223372036854775808 -3 -9223372036854775807 9223372036854775808 3 9223372036854775807 -9223372036854775808 -3 9223372036854775807 -9223372036854775808 3
#include<stdio.h>
#include<string.h>
//1065 A+B and C (64bit)

int real_result(char str1[],char str2[],char str3[]){
    // str表示a+b 的结果
        int i = 0,j = 0;
        char str[30];


        int carry_flags = 0;
        for( j = 0; str1[j] != '\0' && str2[j] != '\0';j++){
            int tmpSum = (str1[j] - '0') + (str2[j] - '0');
            if(carry_flags) tmpSum++;

            if(tmpSum > 9) carry_flags = 1;
            else carry_flags = 0;

            if(carry_flags){
                str[j] = tmpSum % 10 + '0';
            }else{
                str[j] = tmpSum + '0';
            }
        }
        str[j] = '\0';
        //
        for(; str1[j] != '\0'; j++){
            int tmpSum = str1[j] - '0';
            if(carry_flags) tmpSum++;

            if(tmpSum > 9) carry_flags = 1;
            else carry_flags = 0;

            if(carry_flags) str[j] = (tmpSum % 10) + '0';
            else str[j] = tmpSum + '0';
        }
        for(; str2[j] != '\0'; j++){
            int tmpSum = str2[j] - '0';
            if(carry_flags) tmpSum++;

            if(tmpSum > 9) carry_flags = 1;
            else carry_flags = 0;

            if(carry_flags) (tmpSum % 10) + '0';
            else str[j] = tmpSum + '0';
        }

        if(carry_flags){
            str[j] = '1';
            str[++j] = '\0';
        }else{
            str[j] = '\0';
        }

        //compare two strings
        int len_sum = strlen(str),len_c = strlen(str3);
        if(len_sum > len_c) return 1;
        else if(len_sum < len_c) return 0;
        else{ // =
            for(i = len_sum - 1; i >= 0;i--){
                if(str[i] > str3[i]) return 1;
                else if(str[i] == str3[i]) continue;
                else return 0;
            }
            return 2;
        }
}

int two_result(char str_a[],char str_b[],char str_c[]){
    int flag1 = (str_a[0] == '-')? 1 : 0;
    int flag2 = (str_b[0] == '-')? 1 : 0;
    int flag3 = (str_c[0] == '-')? 1 : 0;

    // 反转为数字字符数组,不包括符号'-'
    char new_a[30],new_b[30],new_c[30];
    int i = 0,j = 0;

    for(i = strlen(str_a) -1; i > 0;i--){
        new_a[j++] = str_a[i];
    }
    if(flag1) // a是负数
        new_a[j] = '\0';
    else{
        new_a[j] = str_a[0];
        new_a[++j] = '\0';
    }

    j = 0;
    for(i = strlen(str_b) -1; i > 0;i--){
        new_b[j++] = str_b[i];
    }
    if(flag2) // b is negative
        new_b[j] = '\0';
    else{
        new_b[j] = str_b[0];
        new_b[++j] = '\0';
    }

    j = 0;
    for(i = strlen(str_c) - 1; i > 0;i--){
        new_c[j++] = str_c[i];
    }
    if(flag3) // b is negative
        new_c[j] = '\0';
    else{
        new_c[j] = str_c[0];
        new_c[++j] = '\0';
    }

    if(flag1 == 1 && flag2 == 1 && flag3 == 0){ // - - +
        return 0;
    }
    else if(flag1 ==0 && flag2 == 0 && flag3 ==1 ){ // + + -
        return 1;
    }
    else if(flag1 == 1 && flag2 ==1 && flag3 ==1 ){ //  - - -
        int tmpResult = real_result(new_a,new_b,new_c);
        if(tmpResult == 2 || tmpResult == 1) return 0;
        else return 1;
    }
    else if(flag1 == 0 && flag2 ==0 && flag3 == 0 ){ //  + + +
        int tmpResult = real_result(new_a,new_b,new_c);
        if(tmpResult == 2 || tmpResult == 0) return 0;
        else return 1;
    }
    else if(flag1 == 1 && flag2 ==0){
        if(flag3 == 1){
            int tmpResult = real_result(new_b,new_c,new_a);
            if(tmpResult == 2 || tmpResult == 0) return 0;
            else return 1;
        }else{
            int tmpResult = real_result(new_c,new_a,new_b);
            if(tmpResult == 2 || tmpResult == 1) return 0;
            else return 1;
        }
    }else if(flag1 == 0 && flag2 == 1){
         if(flag3 == 1){
            int tmpResult = real_result(new_a,new_c,new_b);
            if(tmpResult == 2 || tmpResult == 0) return 0;
            else return 1;
        }else{
            int tmpResult = real_result(new_c,new_b,new_a);
            if(tmpResult == 2 || tmpResult == 1) return 0;
            else return 1;
        }
    }

    return 0;
}

int main(){
    char str1[30],str2[30],str3[30];
    int n;
    scanf("%d",&n);
    for(int i =1; i <= n; i++){
        scanf("%s%s%s",str1,str2,str3);

        int result = two_result(str1,str2,str3);
        if(result == 1)
            printf("Case #%d: true\n",i);
        else
            printf("Case #%d: false\n",i);

    }

    return 0;
}

重点是:

1、将不等式恒等变形,不拘泥与a+b>c这个表达式。当b为负数是可以是a>c+(-b),等等。

2、情况要考虑完整,数的符号要识别正确,负数,和非负数;

3、real_result(char str1[],char str2[],char str3[]) 来比较结果大小。考虑进位;

我的测试数据如下:

8
-9223372036854775807 -9223372036854775808 3
Case #1: false
9223372036854775807 9223372036854775808 -100000000000000
Case #2: true
-9223372036854775807 -9223372036854775808 -3
Case #3: false
9223372036854775807 9223372036854775808 3
Case #4: true
-9223372036854775807 9223372036854775808 -3
Case #5: true
-9223372036854775807 9223372036854775808 3
Case #6: false
9223372036854775807 -9223372036854775808 -3
Case #7: true
9223372036854775807 -9223372036854775808 3
Case #8: false

  

猜你喜欢

转载自www.cnblogs.com/hiwjw/p/12654389.html