首页 > 其他 > 详细

实现大整数相加

时间:2018-11-30 18:01:42      阅读:158      评论:0      收藏:0      [点我收藏+]
static String bigNumberSum(String a,String b) {
        char[] charArrayA = new StringBuilder(a).reverse().toString().toCharArray();
        char[] charArrayB = new StringBuilder(b).reverse().toString().toCharArray();
        int length=charArrayA.length > charArrayB.length ? charArrayA.length : charArrayB.length;
        int[] result = new int[length +1];
        
        int temp=0;
        for(int i=0;i<result.length;i++) {
            temp=result[i];
            if(i < charArrayA.length) {
                temp+=charArrayA[i]-‘0‘;
            }
            if(i < charArrayB.length) {
                temp+=charArrayB[i]-‘0‘;
            }
            if(temp >= 10) {
                temp=temp-10;
                result[i+1]=1;
            }
            result[i]=temp;
        }
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<result.length;i++) {
            if(i==length) {
                if(result[i]==0) {
                    break;
                }
            }
            sb.append(result[i]);
        }
        
        return sb.reverse().toString();
    }

 

实现大整数相加

原文:https://www.cnblogs.com/dongma/p/10045617.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!