SMU 算法题:Weijie 的客人名单
题目: 去年的主人公Weijie 要离开海事大学了,于是大家决定帮他举行欢送宴会。 BobWu 很想知道究竟有多少人参加了Weijie 的欢送宴会,于是他弄来了客人名单。名单有N 行,但是其中有很多重复的名字,比方BOBWU > 和BobWu 其实指的都是同一个人,但是名单上的名字却区分大小写重复出现了。 BobWu 想知道去除重复的名字以后究竟有哪些客人。
具体可见:http://mstc.shmtu.edu.cn/oj/problem.php?cid=1003&pid=1
写了半天……结果还是问了师匠:
1#include <stdio.h>
2#include <string.h>
3
4int main(void)
5{
6 int n, i, count, j, found;
7 char cache[200][51];
8 char line[255], name[51];
9
10 gets(line);
11 sscanf(line, "%d", &n);
12
13 for (i = count = 0; i < n; ++i) {
14 gets(name);
15 for (j = found = 0; j < count; ++j) {
16 if (stricmp(cache[j], name) == 0) {
17 found = 1;
18 break;
19 }
20 }
21 if (!found) {
22 strcpy(cache[count++], name);
23 }
24 }
25
26 printf("%d\n", count);
27 for (i = 0; i < count; ++i) {
28 printf("%s\n", cache[i]);
29 }
30
31 return 0;
32}
33
stricmp()
不区分大小写,sscanf()
可扩展阅读:http://www.cnblogs.com/gmh915/archive/2009/09/30/1576995.html
其实效率不高……只是应付题目而已。
评论 (0)