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)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?