zoukankan      html  css  js  c++  java
  • [Jobdu] 题目1037:Powerful Calculator

    题目描述:

        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;
    }
    

      

  • 相关阅读:
    木有晚餐吃的教训暴力图的时候
    HDU1231最大连续子序列DP
    java连连看(GUI有进度条加背景音乐)
    HDU2064简单hanno塔
    HDU1232并查集入门(畅通工程)
    HDU3460Ancient Printer(trie)
    在window下搭建php+apche+masql的方法(个人的蛋疼经历,绝对可靠)
    Java学生管理系统(GUI)(又写了这种破玩意儿了老师,放过我们吧,能不能来点新意)
    VUE使用elpagination添加分页功能
    JS 中深拷贝的几种实现方法
  • 原文地址:https://www.cnblogs.com/easonliu/p/2608574.html
Copyright © 2011-2022 走看看