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了 ...
随机推荐
- 次小生成树(SST)
次小生成树(SST) 题目背景 Awson是某国际学校信竞组的一只菜鸡.Awson最近学了很多最小生成树的算法,Prim算法.Kurskal算法.消圈算法等等.正当Awson洋洋得意之时,信竞组其他大 ...
- Mysql一个表编码的坑,mark一下
问题:一个sql执行很慢,5分钟左右,关键是最大的表是5万出头,另一张表不到5000原因:是两个表的字符集不同,导致匹配时,没有匹配到 解决办法:将两个表的字符集改成一样具体的命令: ALTER TA ...
- Aspose.word组件介绍
阅读目录 1.基本介绍 2.文档对象模型概述 本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4288836.html 本博客其他.NET开 ...
- App配置页面头部
记录一下 App配置页面头部 例 上图红色框部分就是自己配置的头部 //我的客户 "/OACustomer/Index": { title: "我的客户", h ...
- css3响应式表格
<h1>极客学院相关课程</h1> <table class="responsive"> <thead> <tr> &l ...
- Java面向对象(继承、抽象类)
面向对象 今日内容介绍 u 继承 u 抽象类 第1章 继承 1.1 继承的概念 在现实生活中,继承一般指的是子女继承父辈的财产.在程序中,继承描述的是事物之间的所属关系,通过继承可以使多种事物之间形成 ...
- 关于vue-resource 转变成axios的过程
在做东钿贷后系统的时候,我选择了vue-resource这个插件作为与服务器沟通工具,但是听说前端同行说vuejs2.0已经不在维护vue-resource了,vuejs2.0 已经使用了axios了 ...
- Game Engine Architecture
- Java 原型模式(克隆模式)
Java 的设计模式有 23 种,前段时间小编已经介绍了单例模式,由于我们在学习 Spring 的时候在 bean 标签的学习中碰到了今天要讲的原型模式,那么小编就已本文来介绍下原型模式. 原型模 ...
- 【转】ios -- ViewController跳转+传值(方式一)
方式一:通过定义一个实体类传值 (从ViewController1 跳转至 ViewController2) 1.定义实体类NotificationEntity .h声明文件 #import < ...