View Code of Problem 11

import java.util.Scanner;


public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int count = 1;
		while(scanner.hasNext()) {
			int t  = scanner.nextInt();
			if(t == 0) {
				break;
			}
			scanner.nextLine();
			int a=0, x=0, y=0, z=0, x1=0, y1=0, z1=0 ;
			System.out.println("Case #"+count+++":");
			for(int j=0; j<t; j++) {
				String s = scanner.nextLine();
				String[] str =  s.split(" ");
				for(int i=0; i<str.length-1; ) {
					if(str[i].equals("X")) {
						x = x + a;
						x1 = Integer.valueOf(str[++i]);
					}else if(str[i].equals("Y")) {
						y = y + a;
						y1 = Integer.valueOf(str[++i]);
					}else if(str[i].equals("Z")) {
						z = z + a;
						z1 = Integer.valueOf(str[++i]);
					}else {
						a = Integer.valueOf(str[i++]);
					}
				}
				if(x != 0) {
					if(x1 != 0) {
						System.out.print(x*x1+" * "+"X "+"^ "+(x1-1));
					}else {
					System.out.print(x);
				}
				if(y != 0 || z != 0) {
					System.out.print(" + ");
				}
			}
			if(y != 0) {
				if(y1 != 0) {
					System.out.print(y*y1+" * "+"Y "+"^ "+(y1-1));
				}else {
					System.out.print(y);
				}
				if( z != 0) {
					System.out.print(" + ");
				}
			}
			if(z != 0) {
				if(z1 != 0) {
					System.out.print(z*z1+" * "+"Z "+"^ "+(z1-1));
				}else {
				System.out.print(z);
				}
		
			}
			System.out.println();
			a=0; x=0; y=0; z=0; x1=0; y1=0; z1=0;
		}
		}
		scanner.close();
	}
	
}

Double click to view unformatted code.


Back to problem 11