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 x1=0,xn=0,y1=0,yn=0,z1=0,zn=0;
		int i=0,j=0,t=0,len=0;
		int xs=0,zs=0,count=1;
		char[]data = new char[1001];
		char f;
		while(scanner.hasNext()){
			t = Integer.parseInt(scanner.nextLine());
			if(t==0) break;
			System.out.printf("Case #%d:\n",count);
			for(i=0;i<t;i++){
				xs=0;zs=0;x1=0;xn=0;y1=0;yn=0;z1=0;zn=0;len=0;
				String s = scanner.nextLine();
				data = s.toCharArray();
				len=data.length;
				for(j=0;j<len;){
					xs=data[j]-'0';
					f=data[j+2];
					zs=data[j+4]-'0';
					
					if(f=='X') {
						x1+=xs;
						xn=zs;
					}else if (f=='Y'){
						y1+=xs;
						yn=zs;
					}else if(f=='Z'){
						z1+=xs;
						zn=zs;
					}
					xs=data[j];
					j=j+6;
				} 
				
				if(x1*xn!=0) System.out.printf("%d * X ^ %d",x1*xn,xn-1);
				if(x1!=0&&xn==0) System.out.printf("%d",x1);
				if(x1!=0&&y1!=0) System.out.printf(" + ");
				if(y1*yn!=0) System.out.printf("%d * Y ^ %d",y1*yn,yn-1);
				if(y1!=0&&yn==0) System.out.printf("%d",y1);
				if((x1!=0&&z1!=0)||(z1!=0&&y1!=0)) System.out.printf(" + ");
				if(z1*zn!=0) System.out.printf("%d * Z ^ %d",z1*zn,zn-1);
				if(z1!=0&&zn==0) System.out.printf("%d",z1);
				System.out.println();
			}
			count++;
		}
		scanner.close();
	
	}
}

Double click to view unformatted code.


Back to problem 11