UVA-465 Overflow
题目大意:判断第一个因子,第二个因子和结果有没有溢出,运算包括加法乘法
Sample Input
300 + 3
9999999999999999999999 + 11
Sample Output
300 + 3
9999999999999999999999 + 11
first number too big
result too big
解题思路:伪高精度,直接拿去和 int 的最大取值比较即可。
//UVA-465 Overflow
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstdlib>
using namespace std;
const int inf = INT_MAX; //INT_MAX头文件 climits
int main() {
double a, b;
char s1[1010],s2,s3[1010];
while (scanf("%s %c %s",s1,&s2,s3) != EOF) {
printf("%s %c %s\n",s1,s2,s3);
a = atof(s1); //字符串转换为双精度浮点数(double),头文件 cstdlib
b = atof(s3);
if (s2 == '+') {
if (a > inf)
printf("first number too big\n");
if (b > inf)
printf("second number too big\n");
if (a + b > inf)
printf("result too big\n");
}
if (s2 == '*') {
if (a > inf)
printf("first number too big\n");
if (b > inf)
printf("second number too big\n");
if (a * b > inf)
printf("result too big\n");
}
}
return 0;
}