UVa 10815 - Andy's First Dictionary
这道题貌似有陷阱:用gets过不了。
1 # include <stdio.h>
2 # include <ctype.h>
3 # include <string.h>
4 # include <stdlib.h>
5
6 # define MAX_LEN 205
7 # define MAXN 5005
8
9 char dic[MAXN][MAX_LEN];
10 char word[MAX_LEN];
11
12 int cmp(const void *a, const void *b);
13
14 int main()
15 {
16 char ch;
17 int cnt, k, i, in;
18
19 cnt = k = 0;
20 while ((ch=getchar()) != EOF)
21 {
22 if (isalpha(ch)) word[k++] = tolower(ch);
23 else if(isalpha(word[0]))
24 {
25 word[k] = '\0';
26 for (in = i = 0; i < cnt; ++i)
27 if (strcmp(dic[i], word) == 0) {in = 1; break;}
28 if (!in) strcpy(dic[cnt++], word);
29 k = 0;
30 }
31 }
32 qsort(dic, cnt, sizeof(dic[0]), cmp);
33 for (i = 1; i < cnt; ++i)
34 puts(dic[i]);
35
36 return 0;
37 }
38
39 int cmp(const void *a, const void *b)
40 {
41 return strcmp((char *)a, (char *)b);
42 }
gets的做法(WA):
1 # include <stdio.h>
2 # include <ctype.h>
3 # include <string.h>
4 # include <stdlib.h>
5
6 # define MAXN 5005
7 # define MAX_LEN 205
8
9 char line[MAX_LEN];
10 char word[MAX_LEN];
11 char dic[MAXN][MAX_LEN];
12
13 int cmp(char *s1, char *s2);
14
15 int main()
16 {
17 int i, j, k, len, cnt, in;
18
19 cnt = 0;
20 while (gets(line) != NULL)
21 {
22 for (i = 0; line[i] != '\0'; ++i)
23 {
24 if (!isalpha(line[i])) continue;
25 k = 0;
26 while (isalpha(line[i])) word[k++] = tolower(line[i++]);
27 word[k] = '\0';
28 for (in = j = 0; j < cnt; ++j)
29 if (strcmp(dic[j], word) == 0) {in = 1; break;}
30 if(!in) strcpy(dic[cnt++], word);
31 }
32 }
33 qsort(dic, cnt, sizeof(dic[0]), cmp);
34 for (i = 0; i < cnt; ++i)
35 puts(dic[i]);
36
37 return 0;
38 }
39
40 int cmp(char *s1, char *s2)
41 {
42 return strcmp(s1, s2);
43 }
/* */