题目: WUST 零起点学算法92——单词数 Description BobLee 最近忙着考研,话说某一天当他正在看一篇英语阅读时,突然想到想去统计下这篇文章不同单词的个数,由于BobLee很忙,所以想让你帮忙统计一下 Input 有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。(保证每行不超过1000个字符) Output 每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数 Sample Input you are very kind # Sample Output 4 测试代码一:
#include<stdio.h> #include<string.h> #include<stdlib.h> int fun_rem(char *s,char rem[200][100]); int main() { char *s = (char *)calloc(1001, sizeof(char)); char rem[200][100] = {0,}; // to remeber the diffrent word while (gets(s), strcmp(s, "#") != 0) // if s="#" ends the progress { printf("%d\n", fun_rem(s, rem)); } return 0; } int fun_rem(char *s,char rem[200][100]) { unsigned int flag = 1; int i, j, k, lenth = strlen(s), cnt = 0,count=0; char s1[40] = { 0 ,}; for (i = 0,j=0; i < lenth; i++)// to count the different words { if (*(s + i) != ' ') *(s1 + j++) = *(s + i); // put the word into string s1 if(i==lenth-1 || (*(s + i) == ' ' && *(s + i - 1) != ' ')) // memory the words in rem { *(s1 + j) = 0;//set an end to s1 j = 0; //reset the varity j for (k = 0; k < cnt; k++)// to know if the new s1 is diiferent of all the words in rem if (strcmp(s1,*( rem + k)) == 0) flag = 0; if (flag == 1) { strcpy(rem[cnt++], s1); count++; } flag = 1;//reset } } return count; }结果: o j: runtime error
