您的位置:首页 > 其它

UVa 465 Overflow

2014-06-12 13:19 405 查看
上次那个大数开方的高精度的题,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代码。

//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

const int INT = 2147483647;
const int maxn = 300;
char a[maxn], b[maxn], c;

int main(void)
{
#ifdef LOCAL
freopen("465in.txt", "r", stdin);
#endif

double x, y, z;
while(scanf("%s %c %s", a, &c, b) == 3)
{
printf("%s %c %s\n", a, c, b);
x = atof(a);
y = atof(b);
if(c == '+')
z = x + y;
if(c == '*')
z = x * y;
if(x > INT)
cout << "first number too big" << endl;
if(y > INT)
cout << "second number too big" << endl;
if(z > INT)
cout << "result too big" << endl;
}
return 0;
}


可我还是想把自己WA的代码贴出来,毕竟是花了大心思用心去写的。
而且这个代码也通过了样例测试和我自己能想到的各种极端的情况。

//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxn = 300;
const char *INT = "2147483647";
char a[maxn], b[maxn], c[maxn], result[maxn];
int x[maxn], y[maxn], z[maxn];
bool judge(char c[], int l);
void fun(char c[]);

int main(void)
{
#ifdef LOCAL
freopen("465in.txt", "r", stdin);
#endif

char op;
while(gets(c))
{
cout << c << endl;
sscanf(c, "%s %c %s", a, &op, b);
fun(a);
fun(b);
int la = strlen(a);
int lb = strlen(b);
bool flaga, flagb;
if(flaga = judge(a, la))
cout << "first number too big" << endl;
if(flagb = judge(b, lb))
cout << "second number too big" << endl;

int i, j;
//对结果是否溢出进行处理
if(op == '+')
{
if(flaga || flagb)//加法运算有一个加数溢出那么结果溢出
{
cout << "result too big" << endl;
continue;
}
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
for(i = la - 1; i >= 0; --i)
x[la - 1 - i] = a[i] - '0';
for(i = lb - 1; i >= 0; --i)
y[lb - 1 - i] = b[i] - '0';
for(i = 0; i < lb; ++i)//高精度加法运算
{
x[i] = x[i] + y[i];
if(x[i] > 10)
{
j = i;
while(x[j] > 10)//处理连续进位的问题
{
x[j++] -= 10;
++x[j];
}
}
}
}

if(op == '*')
{
//如果乘数为0的话,那么结果不溢出
if((la == 1 && a[0] == '0') || (lb == 1 && b[0] == '0'))
continue;
else
{
if((flaga || flagb) || (la + lb > 11))//有一个乘数溢出或者两乘数位数之和超过11位
{
cout << "result too big" << endl;
continue;
}
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
for(i = la - 1; i >= 0; --i)
x[la - 1 - i] = a[i] - '0';
for(i = lb - 1; i >= 0; --i)
y[lb - 1 - i] = b[i] - '0';

for(i = 0; i < lb; ++i)
{
int s = 0, c = 0;
for(j = 0; j < maxn; ++j)
{
s = x[j] * y[i] + c;
x[i + j] = s % 10;
c = s / 10;
}
}
}
}

for(i = maxn - 1; i >= 0; --i)
if(x[i] != 0)
break;
memset(result, 0, sizeof(result));
j = 0;
for(; i >= 0; --i)
result[j++] = x[i] + '0';//将结果转化为字符串好进行判断
if(judge(result, strlen(result)))
cout << "result too big" << endl;
}
return 0;
}
//判断一个字符串是否会溢出
bool judge(char c[], int l)
{
if(l > 10)
return true;
if(l < 10)
return false;
if(strcmp(c, INT) > 0)
return true;
return false;
}
//忽略字符串的前导0
void fun(char c[])
{
if(c[0] != '0')
return;
int i = 0, j = 0;
while(c[i] == '0')
++i;
for(; c[i] != '\0'; ++i)
c[j++] = c[i];
c[j] = '\0';
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: