Uva 465 Overflow

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=406

表示用double过的太不像话了……分明是高精度的题嘛

输入为两个可能超过int型的非负整数(用a,b表示),运算符只有+和*,那么可以注意到几种需要输出message的情况:

1. 如果a和b某一个超过max_int,那么:和一定超过max_int;如果另一个数不为0时,积超max_int;

2. 如果a,b都不超max_int,那么可以用long long 来计算出和或者积,然后直接和2137383627比较即可。

另外,高精度要注意处理好前导0的问题。

// 13:49 - 14:07
# include <stdio.h>
# include <string.h>
# include <stdlib.h>

char buffer[1050];
char a[505];
char op[5];
char b[505];

int m, n;
bool first, second;

void judge(void)
{
    if (m > 10 || (m==10 && strcmp(a, "2147483647")>0)) {
    first = true;
    puts("first number too big");
    }
    if (n > 10 || (n==10 && strcmp(b, "2147483647")>0)) {
    second = true;
    puts("second number too big");
    }
}
int del(char *s)
{
    int i, k;
    for (i = 0; s[i] == '0'; ++i) ;
    for (k = 0; s[k+i]; ++k) s[k] = s[k+i] ;
    if (k == 0) s[k++] = '0';
    s[k] = '';
    return k;
}
int main()
{
    while (gets(buffer) != NULL) {
    puts(buffer);
    sscanf(buffer, "%s%s%s", a, op, b);
    first = second = false;
    m = del(a);
    n = del(b);
     
    judge(); 
    long long int x = 1, y = 1;
    long long int ct = 2147483647;
    if (!first && !second) {
        x = atoi(a);
        y = atoi(b);        
    }
    if (op[0] == '*' && (strcmp(a, "0") == 0 || strcmp(b, "0") == 0)) {
        ;
    } else {
        if (first || second ) puts("result too big");
        else if (op[0] == '*' && (x*y > ct)) puts("result too big");
        else if (op[0] == '+' && (x+y > ct)) puts("result too big");
    }
    }
    
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/txd0u/p/3389548.html