每天一道算法题

A + B Problem

2016-06-04  本文已影响67人  looper1211

Description:
Input and output are the same with problem 1001.But A and B are big non-negative integers.The biggest integer is less than 10^500.

Input
Input contains multiple test cases.Each case have two big non-negative integers a,b.

Output
For each case,Output a+b.

Sample Input
1234567890987654321
9876543210123456789

123456789123456789
321654987321654987

9081321110693270343633073697474256143563558458718976746753830538032062222085722974121768604305613921745580037409259811952655310075487163797179490457039169594160088430571674960498834085812920457916453747019461644031395307920624947349951053530086146486307198155590763466429392673709525428510973272600608981219760099374675982933766845473509473676470788342281338779191792495900393751209539300628363443012
6538005862664913074813656220643842443844131905754565672075358391135537108795991638155474452610874309742867231360502542308382199053675592825240788613991898567277116881793749340807728335795394301261629479870548736450984003401594705923178314906195914825136973281314862289454100745237769034410057080703111299605127114594552921209928891515242515620324828055912854227507525717981351447473570262981491527798

Sample Output
11111111101111111110
445111776445111776
15619326973358183418446729918118098587407690364473542418829188929167599330881714612277243056916488231488447268769762354261037509129162756622420279071031068161437205312365424301306562421608314759178083226890010380482379311322219653273129368436282061311444171436905625755883493418947294462921030353303720280824887213969228904143695736988751989296795616398194193006699318213881745198683109563609854970810

Hint
Note that there are leading zeros!

解题思路:
首先要解决的就是存储1000位整数的问题。显然,java固有类型的变量都无法保存它。
最直观的想法是可以用一个字符串来保存它。字符串本质上就是一个字符数组,因此为了编程更方便,我们也可以用数组int an[1001]来保存一个1000位的整数
让an[0]存放个位数,an[1]存放十位数,an[2]存放百位数
那么如何实现两个大整数相加呢?
方法很简单,就是模拟小学生列竖式做加法,从个位开始逐位相加,超过或达到10则进位。
也就是说,用int an1[1001]保存第一个数,用int an2[1000]表示第二个数,然后逐位相加,相加的结果直接存放在an1中。

参考代码:

public class Test {
    
    
    static final int MAXNUM = 1000;
    
    int[] numberOne;
    int[] numberTwo;
    
    void init(){
        numberOne = new int[MAXNUM+10];
        numberTwo = new int[MAXNUM+10];
    }
    
    void input(char[] x,char[] y){
        for(int i = x.length,j=0;i>=0;i--){
            numberOne[j++]=x[i]-'0';
        }
        for(int i = y.length,j=0;i>=0;i--){
            numberTwo[j++]=x[i]-'0';
        }
        int max = x.length>y.length?x.length:y.length;
        for(int i = 0;i<max;i++){
            
            numberOne[i]+=numberTwo[i];
            if(numberOne[i]>9){
                numberOne[i]-=10;
                numberOne[i+1]++;
            }
        }
        boolean flag = false;//此变量用于跳过多余的0 
        for (int i = max; i >= 0; i--) {
            if(flag==true){
                System.out.print(numberOne[i]);
            }else if(numberOne[i]!=0){//碰到第一个非0的值,就说明多余的0已经都跳
                System.out.print(numberOne[i]);
                flag = true;
            }
        }
        if(!flag){//结果为0特殊处理 
            System.out.print("0\n");
        }
    }
    
}
上一篇下一篇

猜你喜欢

热点阅读