UVA-465 Overflow
2016-07-19 21:00
363 查看
UVA-465 Overflow
题目大意:判断第一个因子,第二个因子和结果有没有溢出,运算包括加法乘法
Sample Input
300 + 39999999999999999999999 + 11
Sample Output
300 + 39999999999999999999999 + 11
first number too big
result too big
解题思路:伪高精度,直接拿去和 int 的最大取值比较即可。
//UVA-465 Overflow #include <iostream> #include <cstdio> #include <climits> #include <cstdlib> using namespace std; const int inf = INT_MAX; //INT_MAX头文件 climits int main() { double a, b; char s1[1010],s2,s3[1010]; while (scanf("%s %c %s",s1,&s2,s3) != EOF) { printf("%s %c %s\n",s1,s2,s3); a = atof(s1); //字符串转换为双精度浮点数(double),头文件 cstdlib b = atof(s3); if (s2 == '+') { if (a > inf) printf("first number too big\n"); if (b > inf) printf("second number too big\n"); if (a + b > inf) printf("result too big\n"); } if (s2 == '*') { if (a > inf) printf("first number too big\n"); if (b > inf) printf("second number too big\n"); if (a * b > inf) printf("result too big\n"); } } return 0; }
参考了此处 的博客,里面有写关于为什么不直接用 double 型,如果数字太大的话,字符数组存储转化为 double 后应该后面的还会变为 0 ,超过了 double 型最大值。但此时仍比 int 最大值大,但输出的时候需要输出准确值,所以还要用 double 。
相关文章推荐
- UVA-11729 - Commando War(贪心、排序)
- 禁止各浏览器标签的选中功能
- 杭电-2037 今年暑假不AC(sort+结构体+贪心)
- UVA-11292 - Dragon of Loowater(排序)
- 第一天 Add Two Numbers(链表加法)
- centos 7安装配置bind
- 【cf23E】Tree
- Container With Most Water
- 求交集和并集的线性算法
- linux下进度条的简单实现
- (Gym 100685G) Gadget Hackwrench(LCA在线ST)
- springMVC初探--环境搭建和第一个HelloWorld简单项目
- Android即时通讯实现原理
- Python学习笔记(一)—NotePad++与插件的安装和环境配置
- [LeetCode] 31. Next Permutation
- ios开发-沙盒机制-获取路径方法
- Java基础之(八)方法覆盖
- 常用参考网站
- Milk(sort+结构体) hd 1070
- (转)Apache Mina网络框架