uva 465 - Overflow 高精度还是浮点数?
Overflow |
Write a program that reads an expression consisting of two non-negative integer and an operator. Determine if either integer or the result of the expression is too large to be represented as a ``normal'' signed integer (type integer if you are working Pascal, type int if you are working 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 containing as 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
高精度解法:直接套用刘汝佳模版
刚开始总是WA,原因有2:1.没有删除输入字符串的前导0(但是输出原字符串的时候不能删除前导0) 2.数组开太小了。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <climits>
using namespace std; #define maxn 30000 struct bign
{
int len, s[maxn]; bign()
{
memset(s, , sizeof(s));
len = ;
} bign(int num)
{
*this = num;
} bign(const char* num)
{
*this = num;
} bign operator = (int num)
{
char s[maxn];
sprintf(s, "%d", num);
*this = s;
return *this;
} bign operator = (const char* num)
{
len = strlen(num);
for (int i = ; i < len; i++) s[i] = num[len-i-] - '';
return *this;
} string str() const
{
string res = "";
for (int i = ; i < len; i++) res = (char)(s[i] + '') + res;
if (res == "") res = "";
return res;
} bign operator + (const bign& b) const
{
bign c;
c.len = ;
for (int i = , g = ; g || i < max(len, b.len); i++)
{
int x = g;
if (i < len) x += s[i];
if (i < b.len) x += b.s[i];
c.s[c.len++] = x % ;
g = x / ;
}
return c;
} void clean()
{
while(len > && !s[len-]) len--;
} bign operator * (const bign& b)
{
bign c; c.len = len + b.len;
for (int i = ; i < len; i++)
for (int j = ; j < b.len; j++)
c.s[i+j] += s[i] * b.s[j];
for (int i = ; i < c.len-; i++)
{
c.s[i+] += c.s[i] / ;
c.s[i] %= ;
}
c.clean();
return c;
} bign operator - (const bign& b)
{
bign c; c.len = ;
for (int i = , g = ; i < len; i++)
{
int x = s[i] - g;
if (i < b.len) x -= b.s[i];
if (x >= )
g = ;
else
{
g = ;
x += ;
}
c.s[c.len++] = x;
}
c.clean();
return c;
} bool operator < (const bign& b) const
{
if (len != b.len) return len < b.len;
for (int i = len-; i >= ; i--)
if (s[i] != b.s[i]) return s[i] < b.s[i];
return false;
} bool operator > (const bign& b) const
{
return b < *this;
} bool operator <= (const bign& b)
{
return !(b > *this);
} bool operator == (const bign& b)
{
return !(b < *this) && !(*this < b);
} bool operator != (const bign& b)
{
return (b < *this) || (*this < b);
} bign operator += (const bign& b)
{
*this = *this + b;
return *this;
}
}; istream& operator >> (istream &in, bign& x)
{
string s;
in >> s;
x = s.c_str();
return in;
} ostream& operator << (ostream &out, const bign& x)
{
out << x.str();
return out;
} int main()
{
bign a, c, Max = INT_MAX;
char b;
while (cin >> a >> b >> c)
{
cout << a << ' ' << b << ' ' << c << endl;
a.clean(), c.clean();
if (a > Max)
cout << "first number too big" << endl;
if (c > Max)
cout << "second number too big" << endl;
if (b == '+')
if (a+c > Max)
cout << "result too big" << endl;
if (b == '*')
if (a*c > Max)
cout << "result too big" << endl;
}
}
更简单的解法,用浮点数瞬间秒杀。
用atof将string转为float/double.
#include <cstdio>
#include <cstdlib>
#include <climits> char num1[],num2[];
int main()
{
char c;
while (scanf("%s %c %s", num1, &c, num2) != EOF)
{
printf("%s %c %s\n", num1, c, num2);
double a = atof(num1);
double b = atof(num2);
if (a > INT_MAX) printf("first number too big\n");
if (b > INT_MAX) printf("second number too big\n");
if (c == '+' && a+b > INT_MAX) printf("result too big\n");
if (c == '*' && a*b > INT_MAX) printf("result too big\n");
}
}
ps.
float的范围为-2^128 ~ +2^127,也即-3.40E+38 ~ +3.40E+38;
double的范围为-2^1024 ~ +2^1023,也即-1.79E+308 ~ +1.79E+308。
int最大值为INT_MAX(定义在<climits>),为2147483647,即0x7fffffff.
uva 465 - Overflow 高精度还是浮点数?的更多相关文章
- UVa 465 Overflow——WA
上次那个大数开方的高精度的题,UVa113 Power of Cryptography,直接两个double变量,然后pow(x, 1 / n)就A过去了. 怎么感觉UVa上高精度的题测试数据不给力啊 ...
- 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 ...
- 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 ...
- Web Audio介绍
Web Audio还是一个比较新的JavaScript API,它和HTML5中的<audio>是不同的,简单来说,<audio>标签是为了能在网页中嵌入音频文件,和播放器一样 ...
- Python基本语法
目录缩进流程控制语句表达式函数对象的方法类型数学运算 缩进Python开发者有意让违反了缩进规则的程序不能通过编译,以此来强制程序员养成良好的编程习惯.并且Python语言利用缩进表示语句块的开始和退 ...
- 【Swift学习】Swift编程之旅(二)
在本节将介绍一些最基础的知识 swift提供自己版本的类型,下面说明几种简单的类型 Int 整型 Double和float 浮点型 String 字符串型 Bool 布尔型 它也提供了3种主要的强大的 ...
- 03_Swift2基础之基本数据类型+相互转换
1. 整数 整数就是没有小数部分的数字,比如`42`和`-23`.整数可以是`有符号`(正.负.零)或者`无符号`(正.零). Swift 提供了,,和位的有符号和无符号整数类型.这些整数类型和 C语 ...
随机推荐
- Spring IOC、对象依赖关系
Spring IOC.对象依赖关系 2016-09-21 01:36 414人阅读 评论(0) 收藏 举报 本文章已收录于: 版权声明:本文为博主原创文章,未经博主允许不得转载. 引入 Strut ...
- Sketch 介绍
Sketch 插件大集合 -- Using Sketch Like A BOSS 这几天发现了一个叫做 Sketch Toolbox 的 Mac 应用,简直是下载和安装 Sketch 插件 ...
- 纯JS写的2048游戏,分享之
这几天玩儿着2048这个游戏,突然心血来潮想练习下敲代码的思路.于是乎就模仿做了一个,到眼下位置还没有实现动态移动,不是非常好看,只是玩儿着自己模仿的小游戏还是蛮爽的,哈哈 假设没有玩儿过这个游戏,最 ...
- struts 页面调用Action的指定方法并传递参数
如果为action配置了类,那么默认就会执行Action类的excute方法,Action类的写法三种: ① public class Action1 { public String execute( ...
- iOS多线程的初步研究(一)-- NSThread
对于多线程的开发,iOS系统提供了多种不同的接口,先谈谈iOS多线程最基础方面的使用.产生线程的方式姑且分两类,一类是显式调用,另一类是隐式调用. 一.显示调用的类为NSThread.一般构造NSTh ...
- java基础讲解08-----类和对象
1.什么是面向对象? 面向对象设计的实质 就是对现实世界的对象进行建模操作. 现实的生活中,随处可见的一种事物就是对象,对象是事物存在的实体,通常我们将会对对象划分为两个部分,静态部分和动态部分.比如 ...
- python selenium --browser 操作
本节知识点: 打印URL 将浏览器最大化 设置浏览器固定宽.高 操控浏览器前进.后退 打印URL 上一节讲到,可以将浏览器的title打印出来,这里再讲个简单的,把当前URL打印出来.其实也没啥大用, ...
- c# 读取文件流
1.获取文件路径 2.编写读取路径文件信息 private string ReadFileStream(string filePath) { string st ...
- R内存扩展 win7内存扩展
安装包 imdiskinst 文件 램디스크 사용http://www.ltr-data.se/ http://cruciancar.blog.me/150101634586 --TEMP 변수 TE ...
- 【转载】 使用rman进行坏块修复(ORA-01578、ORA-01110)
[转自]http://blog.itpub.net/21256317/viewspace-1062055/ 使用rman进行坏块修复(ORA-01578.ORA-01110) 2012年的一天,处理的 ...