• <input id="auww4"></input>
  • <input id="auww4"><acronym id="auww4"></acronym></input>
  • <input id="auww4"><u id="auww4"></u></input>
    <object id="auww4"><acronym id="auww4"></acronym></object>
    <menu id="auww4"></menu><input id="auww4"><u id="auww4"></u></input>
    <input id="auww4"><u id="auww4"></u></input>
  • F.A.Q
    Hand In Hand
    Online Acmers
    Forum | Discuss
    Statistical Charts
    Problem Archive
    Realtime Judge Status
    Authors Ranklist
     
         C/C++/Java Exams     
    ACM Steps
    Go to Job
    Contest LiveCast
    ICPC@China
    Best Coder beta
    VIP | STD Contests
    Virtual Contests
        DIY | Web-DIY beta
    Recent Contests
    Author ID 
    Password 
     Register new ID

    java不用BigInteger,要繞一萬個彎才能搞出來

    Posted by rooogz at 2020-10-08 17:29:27 on Problem 1002
    (14)  


    import java.util.*;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class H2IO_String {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            String[] twoNum;
            String ans;
            while (in.hasNext()) {
                int n = in.nextInt();
                in.nextLine();
                String[] inputs = new String[n];
                for(int i = 0;i<n;i++) {
                    inputs[i] = in.nextLine();
                }
    
                for(int i = 0;i<n;i++){
                    twoNum = parse2Num(inputs[i]);
                    ans = solve(twoNum);
                    System.out.println("Case " + (i+1)+":");
                    System.out.println(twoNum[0] + " + " + twoNum[1] + " = " + ans);
                    if(i<n-1){
                        System.out.println();
                    }
                }
    
    
            }
    
        }
        public static String[] parse2Num(String line){
            Matcher m_p = Pattern.compile("\\d+").matcher(line);
            String[] ans = new String[2];
            if(m_p.find())
                ans[0] = m_p.group();
            if(m_p.find())
                ans[1]=m_p.group();
    
            return ans;
        }
        public static String solve(String[] nums_ori){
            StringBuilder sb = new StringBuilder();
            String ans;
            int longIndex = 0;
            int shortLen = 0;
            int LongLen = 0;
            int sum=0;
            int carry=0;
            String[] nums = new String[2];
            nums[0] = new StringBuilder(nums_ori[0]).reverse().toString();
            nums[1] = new StringBuilder(nums_ori[1]).reverse().toString();
            if(nums[0].length()>=nums[1].length())
            {
                longIndex =0;
                shortLen= nums[1].length();
                LongLen = nums[0].length();
            }
            else
            {
                longIndex =1;
                shortLen= nums[0].length();
                LongLen = nums[1].length();
            }
            int k = 0;
            while(k<LongLen)
            {
                if(k<shortLen) {
                    sum = nums[0].charAt(k) - '0' + nums[1].charAt(k) - '0' + carry;
                }
                else{
                    sum = nums[longIndex].charAt(k) - '0' + carry;
                }
                if (sum > 9)
                    carry = 1;
                else
                    carry = 0;
                sum = sum%10;
                sb.append(sum);
                k++;
    
            }
            if(carry>0)
                sb.append(carry);
            ans = sb.reverse().toString();
            return ans;
    
        }
    }
    ----------------------------------------------------------------------------
    每行用例用nextLine方法讀出,要格外小心,nextInt是不會消費掉整數后面的分隔符(回車),這導致,下一次用nextLine方法讀了個空行。。。所以要額外調用一下nextLine把空行給消耗掉,然后才能讀到用例那一行。。
    總體思路是把用例行Sting用正則匹配到兩個大整數的String,字符串反轉筆算出答案的字符串,再反轉(汗)
    最后輸出格式冒號后面沒有空格(坑死我了),最后一行輸出沒有空行但是有換行,前面的輸出有換行+空行((lll¬ω¬))


    Post your reply here:

    Author ID
    Password
    Title
    Content  
     
    Hangzhou Dianzi University Online Judge 3.0
    Copyright © 2005-2020 HDU ACM Team. All Rights Reserved.
    Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
    Total 0.000000(s) query 4, Server time : 2020-10-14 09:17:52, Gzip enabled
    棋牌