题解 | #高精度整数加法#

高精度整数加法

http://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6

//罪过罪过,怎么会写出这么丑陋的代码。但运行通过了,手撸代码,没有用库
import java.util.*;
import java.io.*;


public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
        String s=null;
        while((s=bf.readLine())!=null){
            String s1=bf.readLine();
            char chars1[]=s.toCharArray();
            char chars2[]=s1.toCharArray();
            StringBuilder sb=new StringBuilder();
            int flag=0;
            int m=chars1.length<chars2.length?chars1.length:chars2.length;
            int i=chars1.length-1;
            int j=chars2.length-1;
            //58252240597920 5842240597920
            while(i>=0 && j>=0){
                if(flag==0){
                    if(chars1[i]-'0'+chars2[j]-'0'>=10){
                        
                    sb.append((chars1[i]-'0'+chars2[j]-'0'-10));
                        //System.out.println((chars1[i]-'0'+chars2[j]-'0'-10));
                    flag=1;
                    i--;
                    j--;
                    continue;
                    }
                    else {
                        sb.append((chars1[i]-'0'+chars2[j]-'0'));
                        flag=0; 
                        i--;
                        j--;
                        continue;
                    }
                }else if(flag==1){
                    int a=chars1[i]-'0'+chars2[j]-'0'+1;
                    if(chars1[i]-'0'+chars2[j]-'0'+1>=10){
                    sb.append((chars1[i]-'0'+chars2[j]-'0'-10+1));
                    flag=1; 
                    i--;
                    j--;
                    continue;
                    }
                    else {
                        sb.append((chars1[i]-'0'+chars2[j]-'0'+1));
                        flag=0; 
                        i--;
                        j--;
                        continue;
                    }
                }
                
            }
            if(flag==1&&i<0&&j<0)
                sb.append('1');
            else{
                while(flag==1){
                if(i>=0){
                    if(chars1[i]-'0'+1>=10){
                    sb.append('0');
                    flag=1;
                    i--;
                }
                    else {
                        sb.append(chars1[i]-'0'+1);
                        flag=0;
                        i--;
                    }
                }
                 if(j>=0){
                    if(chars2[j]-'0'+1>=10){
                    sb.append('0');
                    flag=1;
                    j--;
                }
                else {
                    sb.append(chars2[j]-'0'+1);
                    flag=0;
                    j--;
                    }
                }
            }
            while(i>=0){
                sb.append(chars1[i]);
                i--;
            }
            while(j>=0){
                sb.append(chars2[j]);
                j--;
            }
            }
            System.out.println(sb.reverse());
        }
    }
}
全部评论

相关推荐

joe2333:怀念以前大家拿华为当保底的日子
点赞 评论 收藏
分享
头像
11-21 11:39
四川大学 Java
是红鸢啊:忘了还没结束,还有字节的5k 违约金
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务