View Code of Problem 11

#include <iostream>
#include <string>
using namespace std;
//题目错的,题目案例1忽略了常数求导后数字应该为0的情况。 
int main(){
	int t,i=1;
	while(cin>>t){
	getchar(); 
	if(t==0) break;	
	cout<<"Case #"<<i<<":"<<endl;
	i++;
	while(t--) { 
		int x1=0,x2=0,y1=0,y2=0,z1=0,z2=0;
		string s;
		getline(cin,s);
		for(string::iterator it=s.begin();it!=s.end();it++){
			if(*it==' '){
				s.erase(it);
				it--;
			}
		} 
		for(int i=0;i<s.size();i+=3){
			if(s[i+1]=='X'){
				x1+=s[i]-'0';
				x2=s[i+2]-'0';
			}
			else if(s[i+1]=='Y'){
				y1+=s[i]-'0';
				y2+=s[i+2]-'0';
			}	
			else{
				z1+=s[i]-'0';
				z2+=s[i+2]-'0';
			}
		}
		if(x1!=0&&x2>0)	printf("%d * X ^ %d",x1*x2,x2-1);
		else if(x1!=0&&x2==0) printf("%d",x1);
		if(x1!=0&&y1!=0) printf(" + ");
		if(y1!=0&&y2>0)	printf("%d * Y ^ %d",y1*y2,y2-1);
		else if(y1!=0&&y2==0) printf("%d",y1);
		if((x1!=0&&z1!=0)||(z1!=0&&y1!=0)) printf(" + ");
		if(z1!=0&&z2>0)	printf("%d * Z ^ %d",z1*z2,z2-1);
		else if(z1!=0&&z2==0) printf("%d",z1);
		cout<<endl;	
		} 
	}	
} 

Double click to view unformatted code.


Back to problem 11