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 高精度还是浮点数?的更多相关文章

  1. UVa 465 Overflow——WA

    上次那个大数开方的高精度的题,UVa113 Power of Cryptography,直接两个double变量,然后pow(x, 1 / n)就A过去了. 怎么感觉UVa上高精度的题测试数据不给力啊 ...

  2. UVA 465 (13.08.02)

     Overflow  Write a program that reads an expression consisting of twonon-negative integer and an ope ...

  3. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  4. Volume 1. Big Number(uva)

    如用到bign类参见大整数加减乘除模板 424 - Integer Inquiry #include <iostream> #include <string> #include ...

  5. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 2(Big Number)

    这里的高精度都是要去掉前导0的, 第一题:424 - Integer Inquiry UVA:http://uva.onlinejudge.org/index.php?option=com_onlin ...

  6. Web Audio介绍

    Web Audio还是一个比较新的JavaScript API,它和HTML5中的<audio>是不同的,简单来说,<audio>标签是为了能在网页中嵌入音频文件,和播放器一样 ...

  7. Python基本语法

    目录缩进流程控制语句表达式函数对象的方法类型数学运算 缩进Python开发者有意让违反了缩进规则的程序不能通过编译,以此来强制程序员养成良好的编程习惯.并且Python语言利用缩进表示语句块的开始和退 ...

  8. 【Swift学习】Swift编程之旅(二)

    在本节将介绍一些最基础的知识 swift提供自己版本的类型,下面说明几种简单的类型 Int 整型 Double和float 浮点型 String 字符串型 Bool 布尔型 它也提供了3种主要的强大的 ...

  9. 03_Swift2基础之基本数据类型+相互转换

    1. 整数 整数就是没有小数部分的数字,比如`42`和`-23`.整数可以是`有符号`(正.负.零)或者`无符号`(正.零). Swift 提供了,,和位的有符号和无符号整数类型.这些整数类型和 C语 ...

随机推荐

  1. ant font 本地化

    要解决的问题1.antd默认iconfont指向的是阿里在公网CDN上部署的url 2.项目需要在本地进行部署,使用的是本地文件的访问方式,希望能内网/离线使用 在ant-design-pro中的配置 ...

  2. 回顾 git 常用命令

    git init      在本地新建一个repo,进入一个项目目录,执行git init,会初始化一个repo,并在当前文件夹下创建一个.git文件夹.   git clone      获取一个u ...

  3. 关于HTML、js加密、混淆、源码保护、代码安全,防止解压直接看源码

    一直有人问HTML加密混淆怎么做,其实这在业内是早已很多人研究过的课题.假日期间整理一篇文章分享给大家. 我们先理下需求,加密的目的是什么?加密到什么级别?为此我们可以牺牲什么?我们知道这个世界不存在 ...

  4. location.href

      location.href用法 CreateTime--2018年2月22日15:22:02 Author:Marydon 1.在当前页面打开URL页面 // 方式一 window.locatio ...

  5. LoadRunner参数化时的各个选项说明

    LoadRunner参数化时的各个选项说明 分类: LoadRunner 2009-03-27 09:32 6294人阅读 评论(1) 收藏 举报 loadrunnerrandomgeneratore ...

  6. 使用 WinEdt 来写中文文章or 建模论文

    找了几乎两个小时…… 后来发现… WinEdt 是可以用来写中文文章的…而并非只能英文文章或演示文稿… \documentclass{article} \usepackage{CJK} \begin{ ...

  7. linux 重启网卡的方法

    http://blog.163.com/drzxqing@126/blog/static/59351445201052392516841/

  8. PHPCMS V9使用中的一些心得体会

    官方演示站:http://v9.demo.phpcms.cn/ 在线帮助文档:http://v9.help.phpcms.cn/ 案例展示:互联网 http://www.i-busi.com 1.   ...

  9. 点滴积累【C#】---错误日志记录到txt文本里。

    效果: 描述:将系统中的错误信息,try catch到日志里面. 代码: [后端代码] using System; using System.Collections.Generic; using Sy ...

  10. ATITIT.翻译模块的设计与实现 api attilax 总结

    ATITIT.翻译模块的设计与实现 api attilax 总结 1. 翻译原理1 2. TMX格式是国际通用格式(xml)1 2.1. 方法/步骤2 3. TRADOS2 4. ATITIT.翻译软 ...