上次那个大数开方的高精度的题,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 = ;
const int maxn = ;
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) == )
{
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 ;
}

代码君

可我还是想把自己WA的代码贴出来,毕竟是花了大心思用心去写的。

而且这个代码也通过了样例测试和我自己能想到的各种极端的情况。

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = ;
const char *INT = "";
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, , sizeof(x));
memset(y, , sizeof(y));
for(i = la - ; i >= ; --i)
x[la - - i] = a[i] - '';
for(i = lb - ; i >= ; --i)
y[lb - - i] = b[i] - '';
for(i = ; i < lb; ++i)//高精度加法运算
{
x[i] = x[i] + y[i];
if(x[i] > )
{
j = i;
while(x[j] > )//处理连续进位的问题
{
x[j++] -= ;
++x[j];
}
}
}
} if(op == '*')
{
//如果乘数为0的话,那么结果不溢出
if((la == && a[] == '') || (lb == && b[] == ''))
continue;
else
{
if((flaga || flagb) || (la + lb > ))//有一个乘数溢出或者两乘数位数之和超过11位
{
cout << "result too big" << endl;
continue;
}
memset(x, , sizeof(x));
memset(y, , sizeof(y));
for(i = la - ; i >= ; --i)
x[la - - i] = a[i] - '';
for(i = lb - ; i >= ; --i)
y[lb - - i] = b[i] - ''; for(i = ; i < lb; ++i)
{
int s = , c = ;
for(j = ; j < maxn; ++j)
{
s = x[j] * y[i] + c;
x[i + j] = s % ;
c = s / ;
}
}
}
} for(i = maxn - ; i >= ; --i)
if(x[i] != )
break;
memset(result, , sizeof(result));
j = ;
for(; i >= ; --i)
result[j++] = x[i] + '';//将结果转化为字符串好进行判断
if(judge(result, strlen(result)))
cout << "result too big" << endl;
}
return ;
}
//判断一个字符串是否会溢出
bool judge(char c[], int l)
{
if(l > )
return true;
if(l < )
return false;
if(strcmp(c, INT) > )
return true;
return false;
}
//忽略字符串的前导0
void fun(char c[])
{
if(c[] != '')
return;
int i = , j = ;
while(c[i] == '')
++i;
for(; c[i] != '\0'; ++i)
c[j++] = c[i];
c[j] = '\0';
}

代码君

UVa 465 Overflow——WA的更多相关文章

  1. uva 465 - Overflow 高精度还是浮点数?

    uva 465 - Overflow  Overflow  Write a program that reads an expression consisting of two non-negativ ...

  2. UVA 465 (13.08.02)

     Overflow  Write a program that reads an expression consisting of twonon-negative integer and an ope ...

  3. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  4. UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据

    题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...

  5. Volume 1. Big Number(uva)

    如用到bign类参见大整数加减乘除模板 424 - Integer Inquiry #include <iostream> #include <string> #include ...

  6. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 2(Big Number)

    这里的高精度都是要去掉前导0的, 第一题:424 - Integer Inquiry UVA:http://uva.onlinejudge.org/index.php?option=com_onlin ...

  7. UVa10815.Andy's First Dictionary

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. POJ 2609 Ferry Loading

    双塔DP+输出路径. 由于内存限制,DP只能开滚动数组来记录. 我的写法比较渣,但是POJ能AC,但是ZOJ依旧MLE,更加奇怪的是Uva上无论怎么改都是WA,其他人POJ过的交到Uva也是WA. # ...

  9. BZOJ 4078: [Wf2014]Metal Processing Plant [放弃了]

    以后再也不做$World Final$的题了................ 还我下午 bzoj上TLE一次后就不敢交了然后去uva交 Claris太神了代码完全看不懂 还有一个代码uva上竟然WA了 ...

随机推荐

  1. 洛谷-P3927 SAC E#1 - 一道中档题 Factorial

    原址 题目背景 数据已修改 SOL君(炉石主播)和SOL菌(完美信息教室讲师)是好朋友. 题目描述 SOL君很喜欢阶乘.而SOL菌很喜欢研究进制. 这一天,SOL君跟SOL菌炫技,随口算出了n的阶乘. ...

  2. Spring的配置及jar包下载

    一.相关说明 IOC: Inversion of Control,控制反转,是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度.其中最常见的方式叫做依赖注入(Dependency In ...

  3. MySQL获取某个时间范围内的数据 TO_DAYS(date)函数

    1.利用to_days函数查询今天的数据: select * from 表名 where to_days(时间字段名) = to_days(now()); to_days函数:返回从0000年(公元1 ...

  4. Kendo MVVM 数据绑定(三) Click

    Kendo MVVM 数据绑定(三) Click Click 绑定可以把由 ViewModel 定义的方法不绑定到目标 DOM 的 click 事件.当点击目标 DOM 元素时触发 ViewModel ...

  5. [拾零]C语言的数组指针

    为了强化记忆,从而写笔记保留. 数组指针,顾名思义,是在说一个指针,这个指针是指向数组的. 区别于指针数组 int* p[5] = NULL; //指针数组 基类型 int* int (*p)[5] ...

  6. Android仿360悬浮小球自定义view实现

    转载请标明出处:http://www.jianshu.com/u/a5ad093cffe8 效果图如下: 图片.png   图片.png 实现当前这种类似的效果 (360小球 悬浮桌面差不错类似).第 ...

  7. LR脚本示例之URL请求(post、get)

    Action(){ //application/x-www-form-urlencoded //application/json //web_add_auto_header("Content ...

  8. jmeter的安装和基本使用

    本篇文章主要介绍一下JMeter的安装及基本使用方法. 1.安装 JMeter的官方网址为http://jmeter.apache.org/ 下载地址为http://jmeter.apache.org ...

  9. Android(java)学习笔记122:BroadcastReceiver之 有序广播和无序广播(BroadcastReceiver优先级)

    之前我们在Android(java)学习笔记178中自定义的广播是无序广播,下面我们要了解一下有序广播: 1. 我们首先了解一下有序广播和无序广播区别和联系? (1)有序广播> 接受者有优先级, ...

  10. 禁止MySQL开机自动启动的方法

    这几天发现电脑卡机变慢了,还有一些卡,发现每次开机MySQL都会自动启动(明明我安装的时候选择了不开机自启,任务管理器启动列表中也没有,但就是自启了...) 1.打开服务列表 有两种方法,一是快捷键 ...