View Code of Problem 11

#include<bits/stdc++.h>
using namespace std;
int main() {
	int n,count,count2,count3,a[1000],b[1000],cas=1,d;
	char c[2000], c1[1000];
	char c2[1000];
	
	while(scanf("%d", &n),n) {
		getchar();//消除回车键 
		d=cas;
		for(int i=0; i<n; i++) {
			gets(c);
			//除去空格 
			count=0; 
			for(int j=0; j<strlen(c); j++) {
				if(c[j]!=' ') c1[count++]=c[j];
			}
			//将字符串每三个放入数组中 
			count2=0;
			for(int j=0; j<count; j+=3) {
				a[count2]=c1[j]-'0';
				c2[count2]=c1[j+1];
				b[count2]=c1[j+2]-'0';
				count2++;
			}
			//合并同类
			for(int j=0; j<count2-1; j++) {
				for(int k=j+1; k<count2; k++) {
					if(c2[j]==c2[k]&&c2[j]!=' ') {
						c2[k]=' ';
						a[j]+=a[k];
					}
				}
			}	
			//求导后输出
			count3=0;	
			for(int j=cas; j<=d; j++)
				cout<<"Case #"<<cas++<<":"<<endl; 
			for(int j=0; j<count2; j++) {
				if(c2[j]!=' ') {
					count3++;
					if(count3==1) {
						if(b[j]-1==0) cout<<a[j]*b[j];
						else if(b[j]==0) cout<<a[j];
						else cout<<a[j]*b[j]<<" * "<<c2[j]<<" ^ "<<b[j]-1;
					}else {
						if(b[j]-1==0) cout<<" + "<<a[j]*b[j];
						else if(b[j]==0) cout<<" + "<<a[j];
						else cout<<" + "<<a[j]*b[j]<<" * "<<c2[j]<<" ^ "<<b[j]-1;
					}
					
				}
			}
			cout<<endl;		
		}

	} 
}

Double click to view unformatted code.


Back to problem 11