UVa 465 Overflow——WA
上次那个大数开方的高精度的题,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型数据。
下面是百科内容:
如果用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的更多相关文章
- uva 465 - Overflow 高精度还是浮点数?
uva 465 - Overflow Overflow Write a program that reads an expression consisting of two non-negativ ...
- UVA 465 (13.08.02)
Overflow Write a program that reads an expression consisting of twonon-negative integer and an ope ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据
题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...
- Volume 1. Big Number(uva)
如用到bign类参见大整数加减乘除模板 424 - Integer Inquiry #include <iostream> #include <string> #include ...
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 2(Big Number)
这里的高精度都是要去掉前导0的, 第一题:424 - Integer Inquiry UVA:http://uva.onlinejudge.org/index.php?option=com_onlin ...
- UVa10815.Andy's First Dictionary
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- POJ 2609 Ferry Loading
双塔DP+输出路径. 由于内存限制,DP只能开滚动数组来记录. 我的写法比较渣,但是POJ能AC,但是ZOJ依旧MLE,更加奇怪的是Uva上无论怎么改都是WA,其他人POJ过的交到Uva也是WA. # ...
- BZOJ 4078: [Wf2014]Metal Processing Plant [放弃了]
以后再也不做$World Final$的题了................ 还我下午 bzoj上TLE一次后就不敢交了然后去uva交 Claris太神了代码完全看不懂 还有一个代码uva上竟然WA了 ...
随机推荐
- 开机报错 the connected AC adapter has a lower wattage than the recommended model which was shipped with the system。
机型:联想Thinkpad T410 报错场景:在电脑插上电源充电情况下开机,会自动进入bios setup utility提示你需要重新设置日期时间.date/time 报错提示:The conne ...
- form-data、x-www-form-urlencoded、raw、binary的区别
1.form-data: 就是http请求中的multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开.既可以上传键值对,也可以上传文件.当上传的字段是文件时 ...
- i-nex安装教程
sudo add-apt-repository ppa:i-nex-development-team/stable sudo apt-get updatesudo apt-get i-nex
- GIT新手入门学习教程
廖雪峰的GIT教程 链接地址:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
- 基于Activiti5.15.1 自定义用户、组(User,Group)实现
基于Activiti5.15.1 自定义用户.组(User,Group)实现 本人刚接触Activiti,最近工作中需要将Activiti中原有的用户,组(ACT_ID_USER,ACT_ID_GRO ...
- 为Oracle Clusterware修改公用及私有网络接口
出于种种原因我们可能需要为已安装的Oracle集群软件修改其使用的公用或私有网络所使用的网络接口(How to Change Interconnect/Public Interface IP or S ...
- 如何使用动画库animate.css
animate.css是一个CSS3动画库,里面预设了抖动(shake).闪烁(flash).弹跳(bounce).翻转(flip).旋转(rotateIn/rotateOut).淡入淡出(fadeI ...
- 实验十三 团队作业9:Beta冲刺与团队项目验收
实验十三 团队作业9:Beta冲刺与团队项目验收 实验时间 2019-6-20(21) Deadline: [6.20-6.26]之间任选连续3天的23:00,以团队随笔博文提交时间为准. 评分标准: ...
- PAT (Basic Level) Practise (中文)- 1007. 素数对猜想 (20)
http://www.patest.cn/contests/pat-b-practise/1007 让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数.显然有 d1=1 且对 ...
- 监控电脑CPU,内存,文件大小,硬盘空间,IP,用户名
public class MonitorTools { /// <summary> /// 获取具体进程的内存,线程等参数情况 /// </summary> /// <p ...