题目1037:Powerful Calculator

题目1037:Powerful Calculator

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:2284

解决:669

题目描述:

    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
来源:
2007年上海交通大学计算机研究生机试真题
答疑:

解题遇到问题?分享解题心得?讨论本题请访问:http://t.jobdu.com/thread-7761-1-1.html


思路:第一次做,麻蛋的没有考虑负数情况,结果就是推倒重来;把加减乘分别封装成函数,直接调用即可,参加运算有负数只需调换位置即可(教训:以后多用函数解决,这样对代码重构很有帮助);最后还是有问题,搞了半天才知道是对结果尾0就直接输出一个0就行。还有马丹的看了别人的代码,以前坚持C++的改用java了,就特么很气,自己终于用C++写出来了。

#include<iostream>
#include<string>
#include<cstring>
#include<fstream>
using namespace std;
int a[405];//存放源操作数
int b[405];//存放目的操作数
int c[1000];//存放结果
void add(int a[],int b[],int c[],int n1,int n2);//处理二数相加
void sub(int a[],int b[],int c[],int n1,int n2);//处理二数相减
void mult(int a[],int b[],int c[],int n1,int n2);//处理二数相乘
int main()
{
    ifstream in;
    in.open("2.txt");
    string str1,str2;
    while(in>>str1>>str2)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        int n1=str1.length();
        int n2=str2.length();
        for(int i=n1-1;i>=0;--i)
            a[i]=str1[n1-1-i]-'0';
        for(int i=n2-1;i>=0;--i)
            b[i]=str2[n2-1-i]-'0';
            if(a[n1-1]>=0 && b[n2-1]>=0)//二个数都是正数
            {
                memset(c,0,sizeof(c));
                add(a,b,c,n1,n2);
                memset(c,0,sizeof(c));
                sub(a,b,c,n1,n2);
                memset(c,0,sizeof(c));
                mult(a,b,c,n1,n2);
            }else if(a[n1-1]>=0 && b[n2-1]<0)//a为正数,b为负数
            {
                memset(c,0,sizeof(c));
                sub(a,b,c,n1,n2-1);
                memset(c,0,sizeof(c));
                add(a,b,c,n1,n2-1);
                memset(c,0,sizeof(c));
                cout<<"-";
                mult(a,b,c,n1,n2-1);
            }else if(a[n1-1]<0 && b[n2-1]>=0)//a为负数,b为正数
            {
                memset(c,0,sizeof(c));
                sub(b,a,c,n2,n1-1);
                memset(c,0,sizeof(c));
                cout<<"-";
                add(a,b,c,n1-1,n2);
                memset(c,0,sizeof(c));
                cout<<"-";
                mult(a,b,c,n1-1,n2);
            }else                              //二个数均为负数
            {
                memset(c,0,sizeof(c));
                cout<<"-";
                add(a,b,c,n1-1,n2-1);
                memset(c,0,sizeof(c));
                sub(b,a,c,n2-1,n1-1);
                memset(c,0,sizeof(c));
                mult(a,b,c,n1-1,n2-1);
            }
    }
    return 0;
}
void add(int a[],int b[],int c[],int n1,int n2)
{
        int temp1=0;
        int temp2=0;
        int carry=0;
        int index=0;
        a[n1]=0;
        b[n2]=0;
        bool flag=false;
        while(temp1<n1 || temp2<n2)
        {
            c[index]=(a[index]+b[index]+carry)%10;
            carry=(a[index]+b[index]+carry)/10;
            ++index;
            ++temp1;
            ++temp2;
        }
        if(carry!=0)
        {
            cout<<carry;
            flag=true;
        }
            for(int i=index-1;i>0;--i)//对首位数是不是0进行判断和处理,很重要
            {                           //之前一直通不过就是这个没有考虑
                if(c[i]!=0 || flag==true)
                {
                   cout<<c[i];
                   flag=true;
                }
            }
            cout<<c[0]<<endl;
}
void sub(int a[],int b[],int c[],int n1,int n2)
{
        int temp1=0;
        int temp2=0;
        int index=0;
        int borrow=0;
        a[n1]=0;
        b[n2]=0;
        bool flag=false;
        while(temp1<n1 || temp2<n2)
        {
            if((a[index]-borrow)>=b[index])
            {
                c[index]=a[index]-borrow-b[index];
                borrow=0;
            }

            else
            {
                c[index]=a[index]-borrow+10-b[index];
                borrow=1;
            }
            ++index;
            ++temp1;
            ++temp2;
        }
        int t;
        if(borrow==1)//对被减数小于减数进行处理
        {
            cout<<"-";
            borrow=0;
            for(int i=0;i<index;++i)
            {
                t=c[i];
                c[i]=(10-borrow-c[i])%10;//这里的c[i]会被更改,所以要用中间变量t
                borrow=1-(10-borrow-t)/10;
            }
        }
            for(int i=index-1;i>0;--i)//对首位数是不是0进行判断和处理,很重要
            {                           //之前一直通不过就是这个没有考虑
                if(c[i]!=0 || flag==true)
                {
                   cout<<c[i];
                   flag=true;
                }
            }
            cout<<c[0]<<endl;
}
void mult(int a[],int b[],int c[],int n1,int n2)
{
        int i;
        int j;
        int temp;
        int carry;
        bool flag=false;
        for(i=0;i<n2;++i)
        {
            carry=0;
            for(j=0;j<n1;++j)
            {
                temp=c[i+j];
                c[j+i]=(a[j]*b[i]+temp+carry)%10;//这里的c[i]会被更改,所以要用中间变量temp
                carry=(a[j]*b[i]+temp+carry)/10;
            }
            c[i+j]=carry;
        }
        if(carry!=0)
        {
            cout<<carry;
            flag=true;
        }
        for(int k=n1+n2-2;k>0;--k)
        {
            if(c[k]!=0 || flag==true)
            {
                cout<<c[k];
                flag=true;
            }
        }
        cout<<c[0]<<endl;
}

(2)来看看垃圾java版本吧

import java.util.Scanner;  
import java.math.BigInteger;  
  
public class Main{    
    public static void main(String[] args){  
        Scanner in=new Scanner(System.in);  
        while(in.hasNext()){  
            BigInteger a=in.nextBigInteger();  
            BigInteger b=in.nextBigInteger();  
            System.out.println(a.add(b));  
            System.out.println(a.subtract(b));  
            System.out.println(a.multiply(b));  
        }  
        in.close();  
    }  
}  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值