九度 oj 题目1037:Powerful Calculator

62 篇文章 0 订阅
5 篇文章 0 订阅

http://ac.jobdu.com/problem.php?pid=1037

参考了

1.王道机试指南


#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
using namespace std;

static const int  maxn =  200;
static const int ten[4] = {1,10,100,1000}; 

typedef struct bigint{ 
    int d[maxn]; 
    bigint(string s){ 
        int len = (int) s.length();    
        int i,j,k;
        for (i = 0; i < maxn; ++i) d[i] = 0; 
        d[0] = (len-1)/4 + 1; 
        for (i = len-1; i >=0; --i) { 
            j = (len-1-i)/4 + 1;
            k = (len-1-i)%4; 
            d[j] += ten[k]*(s[(unsigned) i]-'0'); 
        } 
        while(d[0]>1&&d[d[0]]==0) --d[0]; 
    } 
    bigint(){
        *this = bigint("0"); 
    }  
    string toString(){ 
        string s="";
        int i,j,temp; 
        for (i = 3; i >=0; --i)  
            if(d[d[0]] >= ten[i]) break;  
        temp = d[d[0]];
        for (j = i;  j>=0; --j) { 
            s+= (char)(temp/ten[j] + '0'); 
            temp %= ten[j];
        } 
        for (i = d[0]-1; i >0; --i) { 
            temp = d[i]; 
            for (j=3; j>=0; --j) { 
                s += (char)(temp/ten[j]+'0');   
                temp %=ten[j];
            } 
        } 
        if(s=="") s="0"; 
        return s;
    } 
}BigInt; 

bool operator < (BigInt &a, BigInt &b){ 
    if(a.d[0] != b.d[0]) return a.d[0] < b.d[0];  
    for (int i = a.d[0]; i>=1  ; --i) { 
        if(a.d[i] != b.d[i]) return a.d[i] < b.d[i];  
    } 
    return false;
}  

BigInt operator + (BigInt &a, BigInt &b){ 
    BigInt c; 
    c.d[0] = max(a.d[0],b.d[0]);  
    int i,x=0;
    for (i = 1; i <=c.d[0]; ++i) { 
        x = (a.d[i] + b.d[i] + x);  
        c.d[i] = x%10000;
        x /=10000;
    } 
    while(x!=0){ 
       c.d[++c.d[0]] = x%10000; 
       x /=10000;
    }  
    return c;
}  

BigInt operator - (BigInt &a, BigInt &b){ 
    BigInt c; 
    c.d[0] = a.d[0];
    int i,x=0;
    for (i = 1; i <=c.d[0]; ++i) { 
        x = (10000 + a.d[i] -b.d[i] + x);  
        c.d[i] = x%10000;
        x = x/10000 - 1;
    } 
    while(c.d[0] > 1 && c.d[c.d[0]] == 0) --c.d[0]; 
    return c;
}  

BigInt operator * (BigInt &a, BigInt &b){ 
    BigInt c; 
    c.d[0] = a.d[0]+b.d[0];
    int i,j,x=0;
    for (i = 1; i <=a.d[0]; ++i) { 
        x = 0;
        for (j=1; j<=b.d[0]; ++j) { 
            x = (x + a.d[i]*b.d[j] + c.d[i+j-1]);  
            c.d[i+j-1] = x%10000;
            x/=10000;
        }  
        c.d[i+b.d[0]] = x;
    } 
    while(c.d[0]>1 && c.d[c.d[0]] == 0) --c.d[0]; 
    return c;
}  

int main(){ 
    //freopen("in/1037.in","r",stdin); 
    string a,b; 
    
    bool symbol_a = true,symbol_b = true ;
    while(cin>>a>>b){ 
        symbol_a = true;
        symbol_b = true;
        if(a[0] == '-'){ 
            symbol_a = false; 
            a.erase(0,1);
        }   
        if(b[0] == '-'){ 
            symbol_b = false; 
            b.erase(0,1); 
        }   
        BigInt bigA = BigInt(a);
        BigInt bigB = BigInt(b);
        BigInt bigC; 

        if(symbol_b == symbol_a){
            cout<<(symbol_a?"":"-")<<(bigA+bigB).toString()<<endl;
            
            if(bigA < bigB){
                cout<<(symbol_a?"-":"")<<(bigB-bigA).toString()<<endl;
            }
            else{
                cout<<(symbol_a?"":"-")<<(bigA-bigB).toString()<<endl;
            }
            cout<<(bigA*bigB).toString()<<endl;
        }else{
            if(bigA <bigB){
                
                cout<<(symbol_b?"":"-")<<(bigB-bigA).toString()<<endl;
            }else{
                cout<<(symbol_a?"":"-")<<(bigA-bigB).toString()<<endl;
            }

            cout<<(symbol_a?"":"-")<<(bigA+bigB).toString()<<endl;
            cout<<"-"<<(bigA*bigB).toString()<<endl;
        }

    }  
}  


java :

import java.util.Scanner;
import java.math.BigInteger;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner  sc = new Scanner(System.in);
		while(sc.hasNextBigInteger())
		{
			BigInteger b1 = sc.nextBigInteger();
			BigInteger b2 = sc.nextBigInteger();
			System.out.println(b1.add(b2));
			System.out.println(b1.subtract(b2));
			System.out.println(b1.multiply(b2));			
		}
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值