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语 ...
随机推荐
- selenium-Navigating
The first thing you’ll want to do with WebDriver is navigate to a link. The normal way to do this is ...
- python基础教程_学习笔记12:充电时刻——模块
充电时刻--模块 python的标准安装包含一组模块,称为标准库. 模块 >>> import math >>> math.sin(0) 0.0 模块是程序 不论什 ...
- js 判断是否是IE浏览器及ie版本
方式一:只判断是否是ie浏览器 /** * 判断是否是IE浏览器,支持IE6-IE11 */ function isIE() { //ie? if (!!window.ActiveXObject ...
- 使用DotNetZip压缩与解压缩
下载地址:http://dotnetzip.codeplex.com/ 解压后找到\\DotNetZipLib-DevKit-v1.9\zip-v1.9\Release下的Ionic.Zip.dll文 ...
- mac 使用apache开启https功能,实现ios局域网内测(一)
笔者尝试了网上各种办法最后还是找到了方法解决了一系列局域网内测的问题,随手做个笔记记录下,亲测可行. 一.先生成证书 1.进入apache web 的根目录处理证书命令 cd /Library/Web ...
- android 签名、混淆打包
1.android 签名 使用eclipse导出带签名的apk,最简单的方式. 最后一步finish ,就能导出一个xxx.keystore的文件了. 下次再发布新版本的时候,使用这个生成的签名继续使 ...
- convertView与ViewHolder有什么区别,好处在哪里
convertView 在API中的解释是The old view to reuse, if possible, 第一次getView时还没有convertView,这时你便创建了一个新的vi ...
- 【POJ 3140】 Contestants Division(树型dp)
id=3140">[POJ 3140] Contestants Division(树型dp) Time Limit: 2000MS Memory Limit: 65536K Tot ...
- git 简单使用规范
分支管理办法 创建一个主仓库dev 每个成员fork一份dev分支 在自己fork出来的代码里做开发 开发完成后发出一个合并请求 pull request,等待被其他有合并权限的同事合并代码,合并代码 ...
- memcache概念浅谈及名称混乱之区分
关于memcache这个现在应用广泛的组件,大大提高的网站的响应速度,也方便了程序开发缓存的应用.但是目前针对memcache,网上的资料 大同小异,尤其基于LAMP的网站居多,php/pcel又有两 ...