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. 算法笔记_083:蓝桥杯练习 合并石子(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 在一条直线上有n堆石子,每堆有一定的数量,每次可以将两堆相邻的石子合并,合并后放在两堆的中间位置,合并的费用为两堆石子的总数.求把所有石子 ...

  2. ES6 数值扩展

    1.Number.isNan 和 Number.isFinite Number.isNaN()用来检查一个值是否为NaN Number.isNaN(NaN) // true Number.isNaN( ...

  3. springmvc使用实体參数和ServletAPI

    一. 实体參数 前面我们知道使用注解@RequestParam能够获得參数的值,那么如今提交一个表单怎么获得当中的值了.你能够说能够使用request.getParameter("" ...

  4. Zoho CEO:云计算泡沫巨大 Salesforce仅仅是新的Siebel

    最近Zoho CEO - Sridhar Vembu接受科技博客媒体Diginomica的专訪,从独特的眼光和见解.讲述了云计算行业环境.SaaS公司的生存状态.商业观念以及Zoho的商业模式. Sr ...

  5. Spring bean注入方式

    版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5619525.html  Spring bean提供了3中注入方式:属 ...

  6. 【微信小程序】获取轮播图当前图片下标、滑动展示对应的位数、点击位数展示对应图片

    业务需求: 3个图片轮番播放,可以左右滑动,点击指示点可以切换图片  index.wxml: 这里使用小程序提供的<swiper>组件autoplay:自动播放interval:自动切换时 ...

  7. SpringMVC学习小结

    配置web.xml: <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-cl ...

  8. windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决

    windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决 一.发现问题 由于tomcat内存溢出,在wind ...

  9. Django Error: That port is already in use.

    原文:http://stackoverflow.com/questions/20239232/error-that-port-is-already-in-use A more simple solut ...

  10. django学习笔记【001】django版本的确定&创建一个django工程

    2.3 查看当前的django版本 python3. -m django --version 2.3.1 创建一个django工程 django-admin startproject mysite 在 ...