解题思路: 查找是否有相等的串,若有则标记一下重复串的位置,后面再对这些被标记的串的内容给改成'\0',最后再判断长度大于0的串有多少,即不同单词的个数。
参考代码:
#include <stdio.h> #include <string.h> int main(){ char s[101][101]; int pos[101] = {0}; //记录重复串的位置 int i,j,index = 0,count = 0; while(~scanf("%s", s[index])) index++; for(i = 0; i < index; i++){ for(j = 0; j < index && strlen(s[i]) > 0; j++){ if(strcmp(s[i],s[j]) == 0 && j != i) //加j!=i的原因是为了不让删除只出现过一次的串 pos[j] = 1; //标记 } for(j = 0; j < 101; j++){ if(pos[j] == 1) //是重复串 s[j][0] = '\0'; pos[j] = 0; } } for(i = 0; i < index; i++){ if(strlen(s[i]) > 0) count++; } printf("%d\n", count); return 0; }
0.0分
0 人评分