题目其实不难,但是要注意题目的要求,当前字数(>0)+当前单词长度+1若超过80则需要回车后,输出当前word,并且重新计数。这道题目的数据感觉比较水,不过测试的时候,最后使用fprintf输出在文件中,便于观察。
1 #include <stdio.h> 2 #include <string.h> 3 4 #define MAXNUM 85 5 6 char line[MAXNUM]; 7 char word[MAXNUM]; 8 9 int main() { 10 int i; 11 int num=0, len; 12 //FILE *fout = fopen("data", "w"); 13 14 while (scanf("%s", word) != EOF) { 15 if (strcmp(word, "<br>") == 0) { 16 printf("\n"); 17 //fprintf(fout, "\n"); 18 num = 0; 19 continue; 20 } 21 if (strcmp(word, "<hr>") == 0) { 22 if (num) 23 printf("\n"); 24 //fprintf(fout, "\n"); 25 for (i=0; i<80; ++i) 26 printf("-"); 27 //fprintf(fout, "-"); 28 printf("\n"); 29 //fprintf(fout, "\n"); 30 num = 0; 31 continue; 32 } 33 len = strlen(word); 34 if (num) { 35 if (num+len+1 > 80) { 36 printf("\n%s", word); 37 //fprintf(fout, "\n%s", word); 38 num = len; 39 } else { 40 printf(" %s", word); 41 //fprintf(fout, " %s", word); 42 num += len+1; 43 } 44 } else { 45 printf("%s", word); 46 //fprintf(fout, "%s", word); 47 num += len; 48 } 49 } 50 printf("\n"); 51 //fclose(fout); 52 53 return 0; 54 }