上次那个大数开方的高精度的题,UVa113 Power of Cryptography,直接两个double变量,然后pow(x, 1 / n)就A过去了。

怎么感觉UVa上高精度的题测试数据不给力啊。。。

话说回来,我写了100+行代码,WA了,后来考虑到要忽略前导0,又WA了,实在不知道哪出问题了。

 

 Overflow 

 

Write a program that reads an expression consisting of twonon-negative integer and an operator. Determine if either integer or the resultof the expression is too large to be represented as a ``normal'' signed integer(type integer if you areworking Pascal, type int if you areworking in C).

Input

An unspecified number of lines. Each line will contain an integer,one of the two operators + or *, and another integer.

Output

For each line of input, print the input followed by 0-3 lines containingas many of these three messages as are appropriate: ``first number too big'',``second number too big'',``result too big''.

Sample Input

300 + 3
9999999999999999999999 + 11

Sample Output

300 + 3
9999999999999999999999 + 11
first number too big
result too big

 

看到好多人用atof(),就是把一个字符串转化成double型数据。

下面是百科内容:

 

表头文件 #include <stdlib.h>
定义函数 double atof(const char *nptr);
函数说明 atof()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分,如123.456或123e-2。
返回值 返回转换后的浮点型数。
附加说明 atof()与使用strtod(nptr,(char**)NULL)结果相同。

 

 

如果用double的话,直接将数据和2^31-1进行比较判断是否溢出就好了。

这是别人的AC代码。

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cstdlib>
 6 using namespace std;
 7 
 8 const int INT = 2147483647;
 9 const int maxn = 300;
10 char a[maxn], b[maxn], c;
11 
12 int main(void)
13 {
14     #ifdef LOCAL
15         freopen("465in.txt", "r", stdin);
16     #endif
17 
18     double x, y, z;
19     while(scanf("%s %c %s", a, &c, b) == 3)
20     {
21         printf("%s %c %s\n", a, c, b);
22         x = atof(a);
23         y = atof(b);
24         if(c == '+')
25             z = x + y;
26         if(c == '*')
27             z = x * y;
28         if(x > INT)
29             cout << "first number too big" << endl;
30         if(y > INT)
31             cout << "second number too big" << endl;
32         if(z > INT)
33             cout << "result too big" << endl;
34     }
35     return 0;
36 }
代码君

相关文章: