当前位置: 首页 > 工具软件 > Conquests > 使用案例 >

UVA - 10420 List of Conquests 结构体解法

阎庆
2023-12-01
#include<cstdio>
#include<cstring>
#include<cstdlib>

struct Conquest {
	char *name;
	int number;
};

Conquest con[3000];

int main() {

	int number;
	char str[200];
	int mark = 0;
	scanf("%d\n", &number);
	char temp[3000][100]	;
	for(int i = 0; i < number; i++) {
	char *temp = (char *)malloc(75);
		memset(temp,'\0',sizeof(temp));
		gets(str);	
		int start = 0;
//		str[strlen(str)] = ' ';
//		for(;1 ; start++) {
//			if(str[start] < 'A' ||  (str[start] >'Z' && str[start] < 'a') || str[start] > 'z') 	
//				continue;
//			else 
//				break;
//		}
//
		int out = 0;
		for(int j = 0; ; j++) {
			if((str[j] >= 'A' && str[j] <= 'Z') || (str[j] >= 'a' && str[j] <= 'z')) {
			temp[start] = str[j];
			start++;
			out = 1;
			}
			else if(out) {
				temp[start] = '\0';
				break;
			}
		}
		
	int j2 = 0;	
	for(  ; j2 < mark; j2++) {
			if(strcmp(temp,con[j2].name) == 0)			{
				con[j2].number++;
				break;
			}
			else
				continue;	
		}

		if(j2 == mark) {
			con[j2].name = temp;	
			con[j2].number = 1;
			mark++;
		}
	}

/*	for(int i = 0; i < number - 1; i++) 
		for(int j = i + 1; j < number; j++) {
			char temp2[100];
		if(strcmp(temp[i],temp[j]) > 0) {
			strcpy(temp2,temp[i]);
			strcpy(temp[i],temp[j]);
			strcpy(temp[j],temp2);
		}
		}
		*/
	
	for(int i = 0; i < mark; i++)
		for(int j = i + 1; j < mark; j++)
			if(strcmp(con[i].name,con[j].name) > 0) {
				Conquest co;
				co = con[i];
				con[i] = con[j];
				con[j] = co;	
			}

	for(int i = 0; i < mark; i++)
		printf("%s %d\n",con[i].name, con[i].number);
/*	int num = 1;
	for(int i = 0; i < number; i++) {
		if(strcmp(temp[i],temp[i+1]) != 0 ) {
		printf("%s %d\n",temp[i], num);
		num  = 1;
		}
		else {
			num++;	
		
		}
	}
	*/
	return 0;
}

 类似资料: