View Code of Problem 11

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/*本题思路分两大块,第一个求X,Y,Z的乘数之和,然后判断x的倍数是多少;第二步是讲乘数之和* (倍数-1)。要注意的两点是各个未知数的乘数是否为0以及各个未知数的倍数是否为0,并加以分类*/ 
int main(){
	int t,k=0;
	while(scanf("%d",&t)!=EOF&&t!=0)
	{	getchar();
		int i;
		k++;
		char a[2000];
		printf("Case #%d:\n",k);//主要该处不要加getchar() 
		while(t--)
		{
			int x[2]={0,-1};
			int y[2]={0,-1};
			int z[2]={0,-1};
			scanf("%[^\n]",a);
			getchar(); 
			int len=strlen(a);
			for(i=0;i<len;i++)
			{
				if(a[i]=='X')
				{
					x[0]+=a[i-2]-'0';
					if(a[i+2]=='0')
					{
						a[i+2]='1';
					}
					x[1]=a[i+2]-'0';
					
				}
				else if(a[i]=='Y')
				{
					y[0]+=a[i-2]-'0';
					if(a[i+2]=='0')
					{
						a[i+2]='1';
					}
					y[1]=a[i+2]-'0';
				}
				else if(a[i]=='Z')
				{
					z[0]+=a[i-2]-'0';
					if(a[i+2]=='0')
					{
						a[i+2]='1';
					}
					z[1]=a[i+2]-'0';
				}
			}
			x[0]*=x[1];
			x[1]-=1;
			y[0]*=y[1];
			y[1]-=1;
			z[0]*=z[1];
			z[1]-=1;
			int flag1=0,flag2=0;
			if(x[1]>=0&&y[1]>=0&&x[0]!=0&&y[0]!=0)
			{
				flag1=1;
			}else if(x[1]>=0&&z[1]>=0&&x[0]!=0&&z[0]!=0)
			{
				flag1=1;
			}
			
			if(y[1]>=0&&z[1]>=0&&y[0]!=0&&z[0]!=0)
			{
				flag2=1;
			}
			if(x[1]>0&&x[0]!=0)
			{
				printf("%d * X ^ %d",x[0],x[1]);
			}else if(x[1]==0&&x[0]!=0)
			{
				printf("%d",x[0]);
			}
			
			if(flag1==1)
			{
				printf(" + ");
			}
				if(y[1]>0&&y[0]!=0)
			{
				printf("%d * Y ^ %d",y[0],y[1]);
			}else if(y[1]==0&&y[0]!=0)
			{
				printf("%d",y[0]);
			}
			
			if(flag2==1)
			{
				printf(" + ");
			}
			
			if(z[1]>0&&z[0]!=0)
			{
				printf("%d * Z ^ %d\n",z[0],z[1]);
			}else if(z[1]==0&&z[0]!=0)
			{
				printf("%d\n",z[0]);
			}else
			{
				printf("\n");
			}
			
		
	}
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 11