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了 ...
随机推荐
- JAVA代码之斗地主发牌
理解很好理解,关键是思路 按照斗地主的规则,完成洗牌发牌的动作: 具体规则: 1. 组装54张扑克牌 2. 将54张牌顺序打乱 3. 三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌. ...
- 在CentOS上源码安装Nginx
总步骤: wget http://nginx.org/download/nginx-1.10.1.tar.gz tar -xvf nginx-1.10.1.tar.gz cd nginx-1.10.1 ...
- JavaScript笔记5-事件
一.概述: 事件是可以被JavaScript侦测到的行为.网页中的每个元素都可以产生某些可以触发JavaScript函数的事件.相当于让标签在满足某种条件的时候,调用指定的方法. 二.常用事件 1:o ...
- Jenkins访问路径配置自定义的相对路径
Jenkins安装时没有配置自定义的相对访问路径,例如配置的端口是29957,那访问路径就是http://localhost:29957.以下介绍把访问路径改成http://localhost:299 ...
- 【Android】ContentProvider
转载地址:http://www.cnblogs.com/lqminn/archive/2012/10/16/2725624.html 一.ContentProvider的概念 ContentProvi ...
- [make error ]ubuntu显示不全
make时候,输出到文件里 make >&makelog 就会自动出现一个makelog 会慢一些,不要急.
- django之模版的自定义函数
- 自定义函数 simple_tag a. app下创建templatetags目录 b. 任意xxoo.py文件 c. 创建template对象 register d. __author__ = ' ...
- selenium-Python之上传文件
对于web 页面的上传功能实现一般有一下两种方式 普通上传:普通的附件上传是将本地文件的路径作为一个值放在input标签中,通过form表单将这个值提交给服务器 插件上传:一般是指基于flash.ja ...
- Web端 年月日下拉表 密码判断 按钮判断是否提交
生日: <asp:DropDownList ID="selYear" runat="server"></asp:DropDownList> ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] A A Problem about Polyline(数学)
题目中给出的函数具有周期性,总可以移动到第一个周期内,当然,a<b则无解. 假设移动后在上升的那段,则有a-2*x*n=b,注意限制条件x≥b,n是整数,则n≤(a-b)/(2*b).满足条件的 ...