Uva 10420 - List of Conquests(排序水题)
Problem B
List of Conquests
Input: standard input
Output: standard output
Time Limit: 2 seconds
In Act I, Leporello is telling Donna Elvira about his master's long list of conquests:
``This is the list of the beauties my master has loved, a list I've made out myself: take a look, read it with me. In Italy six hundred and forty, in Germany two hundred and thirty-one, a hundred in France, ninety-one in Turkey; but in Spain already a thousand and three! Among them are country girls, waiting-maids, city beauties; there are countesses, baronesses, marchionesses, princesses: women of every rank, of every size, of every age.'' (Madamina, il catalogo è questo)
As Leporello records all the ``beauties'' Don Giovanni ``loved'' in chronological order, it is very troublesome for him to present his master's conquest to others because he needs to count the number of ``beauties'' by their nationality each time. You are to help Leporello to count.
Input
The input consists of at most 2000 lines, but the first. The first line contains a number n, indicating that there will be n more lines. Each following line, with at most 75 characters, contains a country (the first word) and the name of a woman (the rest of the words in the line) Giovanni loved. You may assume that the name of all countries consist of only one word.
Output
The output consists of lines in alphabetical order. Each line starts with the name of a country, followed by the total number of women Giovanni loved in that country, separated by a space.
Sample Input
3
Spain Donna Elvira
England Jane Doe
Spain Donna Anna
Sample Output
England 1
Spain 2
Problem-setter: Thomas Tang, Queens University, Canada
“Failure to produce a reasonably good and error free problem set illustrates two things a) Lack of creativity b) Lack of commitment”
#include<stdio.h> #include<string.h> typedef struct{ char name[75]; int sum; }beaut; beaut country[2010]; int main() { int i, j, t, n, flag, temp, cnt = 0; char input[80], name[75]; for(i=0; i<2010; ++i) { memset(country[i].name, '\0', sizeof(char)*75); country[i].sum = 0; } scanf("%d", &n); getchar(); for(t=1; t<=n; ++t) { fgets(input, 80, stdin); sscanf(input, "%[^ ]", name); if(cnt == 0) { strcpy(country[cnt].name, name); country[cnt++].sum++; } else { for(i=0; i<cnt && strcmp(country[i].name, name); ++i); if(i >= cnt) { strcpy(country[cnt].name, name); country[cnt++].sum++; } else country[i].sum++; } } for(i=0; i<cnt-1; i++) { flag = 0; for(j=0; j<cnt-1-i; ++j) { if(strcmp(country[j].name, country[j+1].name) > 0) { flag = 1; memset(name, '\0', sizeof(name)); strcpy(name, country[j].name); strcpy(country[j].name, country[j+1].name); strcpy(country[j+1].name, name); temp = country[j].sum; country[j].sum = country[j+1].sum; country[j+1].sum = temp; } } if(!flag) break; } for(i=0; i<cnt; ++i) printf("%s %d\n", country[i].name, country[i].sum); return 0; }
解题报告:
因为没看清题目,贡献了一次WA!
更多内容请关注个人微信公众号 物役记 (微信号:materialchains)