View Code of Problem 11

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int count  = 0;
        while(true)
        {
            int t = sc.nextInt();
            sc.nextLine();
            if (t == 0) break;


            System.out.println("Case #" + (++count) +":");
            for (int i = 0; i < t; ++i) {

                int x_1 = 0, x_exp = 0;
                int y_1 = 0, y_exp = 0;
                int z_1 = 0, z_exp = 0;

                String line = sc.nextLine();
                String[] strs = line.split(" ");
                for (int j = 0; j < strs.length; ++j) {
                    if ("X".equals(strs[j])) {
                        x_1 += Integer.parseInt(strs[j - 1]);
                        x_exp = Integer.parseInt((strs[j + 1]));
                        if (x_exp == 0) x_exp = 1;
                    } else if ("Y".equals(strs[j])) {
                        y_1 += Integer.parseInt(strs[j - 1]);
                        y_exp = Integer.parseInt((strs[j + 1]));
                        if (y_exp == 0) y_exp = 1;
                    } else if ("Z".equals(strs[j])) {
                        z_1 += Integer.parseInt(strs[j - 1]);
                        z_exp = Integer.parseInt((strs[j + 1]));
                        if (z_exp == 0) z_exp = 1;
                    }

                }

                x_1 *= x_exp; --x_exp;
                y_1 *= y_exp; --y_exp;
                z_1 *= z_exp; --z_exp;

                if (x_1 != 0) {
                    if (x_exp != 0) System.out.print(x_1 + " * X ^ " + x_exp);
                    else            System.out.print(x_1);

                    if (y_1 != 0 || z_1 != 0)
                        System.out.print(" + ");
                }

                if (y_1 != 0) {
                    if (y_exp != 0) System.out.print(y_1 + " * Y ^ " + y_exp);
                    else            System.out.print(y_1);

                    if (z_1 != 0)
                        System.out.print(" + ");
                }

                if (z_1 != 0) {
                    if (z_exp != 0) System.out.print(z_1 + " * Z ^ " + z_exp);
                    else            System.out.print(z_1);
                }

                System.out.println();
            }
        }
    }
}

Double click to view unformatted code.


Back to problem 11