Powerful Calculator(带符号的大整数的加减乘)

时间限制:1秒  空间限制:65536K  热度指数:393
 算法知识视频讲解

题目描述

    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.
示例1

输入

20000000000000000
4000000000000000

输出

24000000000000000
16000000000000000

80000000000000000000000000000000

大整数的加法,减法乘法

// pat.cpp : 定义控制台应用程序的

//#include "stdafx.h"
#include"stdio.h"
#include<iostream>
#include<string>
#include<cstring>

using namespace std;
typedef long long ll;
const int maxn=1010;
struct bign{
	int d[1000];
	int len;
	bign(){
	memset(d,0,sizeof(d));
	len=0;
	
	}

};
int sig1,sig2;
bign change(string s){
     bign b;
	 for(int i=0;i<s.length();i++)
		 b.d[b.len++]=s[s.length()-1-i]-'0';
	 return b;



}
bign add(bign a,bign b){
	
    int k=0;bign c;
	for(int i=0;i<a.len||i<b.len;i++)
	{k=a.d[i]+b.d[i]+k;
	c.d[c.len++]=k%10;
	k/=10;
	}
	if(k)c.d[c.len++]=k;
	return c;
	
}

bign sub(bign a,bign b){
    bign c;int k=0;

	for(int i=0;i<a.len;i++){
		c.d[c.len]=a.d[i]-b.d[i];
		if(c.d[c.len]<0){c.d[c.len]+=10;a.d[i+1]-=1;}//如果不够减,向高位借一
		c.len++;
	}

	while(c.len>1&&c.d[c.len-1]==0)c.len--;//这一句一定要加,因为有可能结果的最高位为0
	return c;
}
bign mul(bign a,bign b){
      bign c;c.len=a.len+b.len;//两个数相乘的结果最多的位数是两个数的位数之和
	  for(int i=0;i<a.len+b.len;i++){
		  
		  for(int j=0;j<b.len&&j<=i;j++){
			  c.d[i]+=a.d[i-j]*b.d[j];
			  			  
		  }
		
                        c.d[i+1]=c.d[i]/10;
			  c.d[i]%=10;

} while(c.len>0&&c.d[c.len-1]==0)c.len--; return c;}void output(bign b){for(int i=b.len-1;i>=0;i--)cout<<b.d[i];cout<<endl;}int main(){//freopen("c://jin.txt","r",stdin); string s1,s2;while(cin>>s1>>s2){//带符号的两个大整数加减乘除一要考虑符号,二要考虑两个大整数的绝对值大小if(s1[0]=='-'){sig1=-1;s1.erase(s1.begin());}else sig1=1;if(s2[0]=='-'){sig2=-1;s2.erase(s2.begin());}else sig2=1;while(s1.length()>0&&s1[0]=='0')s1.erase(s1.begin());while(s2.length()>0&&s2[0]=='0')s2.erase(s2.begin());int flag=0;if(s2.length()>s1.length()){flag=1;//比较两个大整数的绝对值大小,第一比较长短}else if(s2.length()==s1.length()&&s2>s1){//第二依次从高位到地位比较大小flag=1;}bign a=change(s1);bign b=change(s2);bign c1=add(a,b);bign c2;if(flag==0)c2=sub(a,b);else c2=sub(b,a); bign c3=mul(a,b);if(sig1==sig2){if(sig1==-1)//如果符号相同而且都为负号{cout<<"-";output(c1);if(flag==0)cout<<"-";output(c2);}else {output(c1);if(flag==1)cout<<"-";output(c2);}output(c3);}else{if(sig1==1){//如果符号不同,而且第一个数是正数if(flag==1)cout<<"-";output(c2);output(c1);}else{if(flag==0)cout<<"-";output(c2);cout<<"-";output(c1);} cout<<"-";output(c3);}}//freopen("CON","r",stdin);//system("pause");return 0;}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值