题目描述:

Today, facing the rapid development of business, SJTU recognizes that more powerful calculator should be studied, developed and appeared in future market shortly. SJTU now invites you attending such amazing research and development work.
    In most business applications, the top three useful calculation operators are Addition (+), Subtraction (-) and Multiplication (×) between two given integers. Normally, you may think it is just a piece of cake. However, since some integers for calculation in business application may be very big, such as the GDP of the whole world, the calculator becomes harder to develop.
    For example, if we have two integers 20 000 000 000 000 000 and 4 000 000 000 000 000, the exact results of addition, subtraction and multiplication are:
    20000000000000000 + 4000000000000000 = 24 000 000 000 000 000
    20000000000000000 - 4000000000000000 = 16 000 000 000 000 000
    20000000000000000 × 4000000000000000 = 80 000 000 000 000 000 000 000 000 000 000
    Note: SJTU prefers the exact format of the results rather than the float format or scientific remark format. For instance, we need "24000000000000000" rather than 2.4×10^16.
    As a programmer in SJTU, your current task is to develop a program to obtain the exact results of the addition (a + b), subtraction (a - b) and multiplication (a × b) between two given integers a and b.

输入:

Each case consists of two separate lines where the first line gives the integer a and the second gives b (|a| <10^400 and |b| < 10^400).

输出:

For each case, output three separate lines showing the exact results of addition (a + b), subtraction (a - b) and multiplication (a × b) of that case, one result per lines.

样例输入:
20000000000000000
4000000000000000
样例输出:
24000000000000000
16000000000000000
80000000000000000000000000000000

大数乘法:a×b=c。a,b,c用string保存,先将a,b reverse一下遍于操作,乘法过程中将a的第i位与b的第j位相乘的结果相加到c的第i+j中,若有进位保存在carry中。

#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;/************** Multi *********************/
string multi(string aa,string bb){string a = aa,b=bb,c;int la,lb;la = a.length();lb = b.length();c.resize(la+lb);for(int i=0;i<la+lb;i++){c[i] = '0';}//c[la+lb] = '\0';int carry = 0,v;reverse(a.begin(),a.end());reverse(b.begin(),b.end());for(int i=0;i<a.length();i++){for(int j=0;j<b.length();j++){v = carry + (a[i]-'0')* (b[j]-'0') + (c[i+j]-'0');c[i+j] = '0' + v%10;carry = v/10;if(j==b.length()-1){c[i+j+1] = '0' + carry;carry = 0;}}}reverse(c.begin(),c.end());return c;
}/************** Add *********************/
string add(string aa,string bb){string a = aa,b=bb,c;reverse(a.begin(),a.end());reverse(b.begin(),b.end());int la,lb;la = a.length();lb = b.length();int lc = la>lb?la:lb;int v;c.resize(0);int carry = 0;for(int i=0;i<lc;i++){if(i>=la&&i<lb){v = carry + (b[i]-'0');}else if(i>=lb&&i<la){v = carry + (a[i]-'0');}else if(i<la&&i<lb){v = carry + (a[i]-'0') + (b[i]-'0');}c.push_back('0' + v%10);carry = v/10;if(i==lc-1){c.push_back(carry + '0');carry = 0;}}reverse(c.begin(),c.end());return c;
}/************** Sub *********************/
string sub(string aa,string bb){string a = aa,b=bb,c;reverse(a.begin(),a.end());reverse(b.begin(),b.end());c.resize(0);int borrow = 0;bool sign = false;int v;if(a.length()<b.length()){swap(a,b);sign = true;}else if(a.length()==b.length()){for(int i=a.length()-1;i>=0;i--){if(a[i]<b[i]){swap(a,b);sign = true;break;}}}for(int i=0;i<a.length();i++){if(i>=b.length()){v = (a[i] - '0') - borrow;if(v>=0){c.push_back(v+'0');borrow = 0;}else{v += 10;borrow = 1;c.push_back('0'+v);}}else{v = (a[i]-'0') - (b[i] - '0') - borrow;if(v>=0){c.push_back(v+'0');borrow = 0;}else{v += 10;borrow = 1;c.push_back('0'+v);}}}if(sign){c = c + "-";}reverse(c.begin(),c.end());return c;
}void print(string s){bool flag = false;for(int i=0;i<s.length();i++){if((s[i]!='0'&&s[i]!='-')||(s[i]=='0'&&flag)){flag = true;cout<<s[i];}if(s[i]=='-'){cout<<s[i];}}if(!flag){cout<<"0";}cout<<endl;
}int main(int argc,char* argv[]){string a,b,c;while(cin>>a>>b){c = add(a,b);print(c);c = sub(a,b);print(c);c = multi(a,b);print(c);}return 0;
}

  

[Jobdu] 题目1037:Powerful Calculator相关推荐

  1. 九度OJ 1037:Powerful Calculator(强大的计算器) (大整数运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1821 解决:528 题目描述: Today, facing the rapid development of business, SJTU ...

  2. [Jobdu] 题目1504:把数组排成最小的数

    题目描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 输入: 输 ...

  3. [Jobdu] 题目1214:丑数

    题目描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 输入: ...

  4. [Jobdu] 题目1500:出操队形

    题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往楼下跑了,然后身高矮的排在队伍的前面,身高较高的就要排在队尾.突然,有一天出操负责人想了一个 ...

  5. [Jobdu] 题目1337:寻找最长合法括号序列

    题目描述: 给你一个长度为N的,由'('和')'组成的括号序列,你能找出这个序列中最长的合法括号子序列么?合法括号序列的含义便是,在这个序列中,所有的左括号都有唯一的右括号匹配:所有的右括号都有唯一的 ...

  6. [Jobdu] 题目1499:项目安排

    题目描述: 小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时间,假设做完每个项目后,拿到报酬都是不同的.由于小明马上就要硕士毕业了,面临着买房.买车.给 ...

  7. [Jobdu] 题目1530:最长不重复子串

    题目描述: 最长不重复子串就是从一个字符串中找到一个连续子串,该子串中任何两个字符都不能相同,且该子串的长度是最大的. 输入: 输入包含多个测试用例,每组测试用例输入一行由小写英文字符a,b,c... ...

  8. [Jobdu] 题目1527:首尾相连数组的最大子数组和

    题目描述: 给定一个由N个整数元素组成的数组arr,数组中有正数也有负数,这个数组不是一般的数组,其首尾是相连的.数组中一个或多个连续元素可以组成一个子数组,其中存在这样的子数组arr[i],-arr ...

  9. 题目 1037: [编程入门]宏定义的练习

    题目描述 输入两个整数,求他们相除的余数.用带参的宏来实现,编程序. 输入 a b两个数 输出 a/b的余数 样例输入 3 2 样例输出 1 import java.util.Scanner; pub ...

  10. [LeetCode]Basic Calculator

    题目:Basic Calculator 给定一个合法的运算表达式,该表达式中只包含数字.'+'.'-'.' '.'('.')'. 思路: 简单思考不用看成加减两种运算,直接看成加法,只不过由正负: 如 ...

最新文章

  1. sqlserver 自增ID插入指定数据
  2. 学界 | 数据并行化对神经网络训练有何影响?谷歌大脑进行了实证研究
  3. 今天收到ORACLE 10G OCA 证书!
  4. 放弃 Windows 后 ,开源操作系统能成为主流桌面系统吗?
  5. java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
  6. lingo入门(数据部分)
  7. 做到阿里P7和考上985/211哪个更难?
  8. mysql防注入插件_mybatis-plus插件使用的一些问题
  9. 关于int main(int argc,char* argv[])详解
  10. c语言大学程序设计题库,c语言程序设计题库
  11. android word缺少字体下载,OfficeSuite字体包
  12. 网页源代码保护(禁止右键、复制、另存为、查看源文件)
  13. android如何实现环形缓冲区
  14. LaTeX各种算法排版
  15. 计算机主板功能是什么,电脑主板的作用是什么_电脑主板作用详细介绍 - 系统家园...
  16. 数学不好,进入机器学习领域还有戏吗?
  17. 微信公众平台开发 上传下载多媒体文件
  18. php插入音乐代码,如何修改Wordpress博客代码在文章中插入音乐 | 垃圾站
  19. 百一测评网站切屏检测绕过
  20. 合泰杯比赛总结(一)

热门文章

  1. 创建Docker私有仓库
  2. Linux下安装matlab2014a
  3. office如何去除多页签
  4. 学习pytorch: 数据加载和处理
  5. 在Mac上如何设置自动解压下载的压缩文件?
  6. 视频软件会声会影支持哪些视频格式?
  7. iOS底层探索之多线程(十五)—@synchronized源码分析
  8. 关于iOS13 的一些适配
  9. jquery扩展方法的两种形式
  10. cdr如何制作图表?