View Code of Problem 16

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int n = Integer.parseInt(sc.nextLine());
        Map<String, Integer> map = new HashMap<>();
        map.put("zero", 0);
        map.put("one", 1);
        map.put("two", 2);
        map.put("three", 3);
        map.put("four", 4);
        map.put("five", 5);
        map.put("six", 6);
        map.put("seven", 7);
        map.put("eight", 8);
        map.put("nine", 9);

        String[] strings = {"zero", "one","two","three","four","five","six", "seven","eight","nine"};

        while (n-- > 0) {
            int mode = Integer.parseInt(sc.nextLine());
            String input = sc.nextLine();
            String[] eles = input.trim().split(" ");
            int a_op = 0, b_op = 0;
            int i = 0;
            while (!"+".equals(eles[i])) {
                if (map.get(eles[i]) == null) {
                    a_op = Integer.parseInt(eles[i]);
                } else
                    a_op = a_op * 10 + map.get(eles[i]);
                ++i;
            }
            ++i;
            while (!"=".equals(eles[i])) {
                if (map.get(eles[i]) == null) {
                    b_op = Integer.parseInt(eles[i]);
                } else
                    b_op = b_op * 10 + map.get(eles[i]);
                ++i;
            }

            int sum = a_op + b_op;
            char[] chars = (sum + "").toCharArray();
            if (mode == 0)
                System.out.println(sum);
            else {

                for (int j = 0; j < chars.length - 1; ++j)
                    System.out.print(strings[chars[j] - '0'] + " ");
                System.out.println(strings[chars[chars.length - 1] - '0']);
            }
        }
    }
}

Double click to view unformatted code.


Back to problem 16