Foregone Solution

原题地址: https://codingcompetitions.withgoogle.com/codejam/round/0000000000051705/0000000000088231

看到4就拆分成2+2, 看别的就0+x, 注意leading zero.

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.valueOf(scanner.nextLine());
        for (int i = 1; i <= n; i++) {
            String[] res = solve(scanner.nextLine());
            System.out.println("Case #" + i + ": " + res[0]+ " " + res[1]);
        }
    }

    private static String[] solve(String n){
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        for(int i = 0; i < n.length(); i++) {
            if (n.charAt(i) == '4'){
                sb1.append('1');
                sb2.append('3');
            }
            else{
                sb1.append(n.charAt(i));
                sb2.append('0');
            }
        }
        int count  = 0;
        while(count < sb2.length() && sb2.charAt(count) == '0') {
            count++;
        }
        sb2.delete(0, count);
        if (sb2.length() == 0) {
            sb2.append('0');
        }
        return new String[]{sb1.toString(), sb2.toString()};
        };
    }