View Code of Problem 36

#include<stdio.h>

void swap1(char *a, char *b) {
	char t;
	t=*a;
	*a=*b;
	*b=t;
}

void swap2(int *a, int *b) {
	int t;
	t=*a;
	*a=*b;
	*b=t;
}

int main() {
	int T,count[30]={0},s1,s2,s3,flag;
	char c[1000],arr[30];
	scanf("%d", &T);
	while(T--) {
		scanf("%s", &c); 
		s1=0;
		s2=0;
		while(c[s1]!='\0') s1++;
		// 统计字母个数 
		for(int i=0; i<s1; i++) {
			flag=1;
			for(int k=i-1; k>=0; k--) {
				if(c[k]==c[i]) {
					flag=0;
					break;
				}
			}
			if(flag==1) {
				s3=1;
				for(int j=i+1; j<s1; j++) {
					if(c[j]==c[i]) s3++;
				}
				arr[s2]=c[i];
				count[s2]=s3;
				s2++;
			}
		}
		// 将字母排序 (冒泡)
		flag=0;
		for(int i=0; i<s2-1; i++) {
			for(int j=0; j<s2-i-1; j++) {
				if(arr[j]>arr[j+1]) {
					swap1(&arr[j],&arr[j+1]);
					swap2(&count[j],&count[j+1]);
					flag=1;
				}
			}
			if(flag==0) break; // 优化 
		}
		// 打印 
		for(int i=0; i<s2; i++) printf("%d%c", count[i], arr[i]);
		printf("\n");
	}
} 

Double click to view unformatted code.


Back to problem 36