E - 不同的单词

xiaoxiao2021-02-28  12

E - 不同的单词

给出一个英文单词的列表,计算有多少不同的单词在列表中。 Input

本题有多组输入数据,你必须处理到EOF为止

每组数据的第一行有一个整数n, 1<=n<=1000.下面的n行每行一个单词,每个单词的长度不超过20。单词大小写忽略。

Output 每组数据输出一个整数,表示不同的单词数。 Sample Input 5 FZU FzU LOY BNh FZU Sample Output 3

题解:先将所有字符串存到字符数组中,再对单词进行转换,把所以单词转换为大写,再将相同的单词中只留一个,其他全部改成“11”,最后统计字符数组中不是“11”的单词数。

#include <stdio.h> #include <string.h> const char m[5] = "11"; void change(char s[][25],int n) { int i,j; for(i = 0;i < n;i++) { for(j = 0;s[i][j] != '\0';j++) { if(s[i][j] >= 'a' && s[i][j] <= 'z') { s[i][j] -= 32; } } } } void fn(char s[][25],int n) { int i,j; for(i = 0;i < n - 1;i++) { for(j = i + 1;j < n;j++) { if(strcmp(s[j],m) && strcmp(s[i],s[j]) == 0) { strcpy(s[j],m); } } } } int sum(char s[][25],int n) { int i; int count = 0; for(i = 0;i < n;i++) { if(strcmp(s[i],m)) { count++; } } return count; } int main() { int n; int i; int count; while(scanf("%d",&n) != EOF ) { char s[1005][25]; for(i = 0;i < n;i++) { scanf("%s",s[i]); } change(s,n); fn(s,n); count = sum(s,n); printf("%d\n",count); } return 0; }

转载请注明原文地址: https://www.6miu.com/read-2596150.html

最新回复(0)