POJ 2271 HTML

来源:互联网 发布:知乎三角铁十级什么梗 编辑:程序博客网 时间:2024/06/04 18:42

题目大意:

        要求翻译一篇HTML文档,文档中只有两种HTML标记<br>和<hr>,分别表示换行和划线板,并将左右连续的空格、制表符、换行符当做一个空格输出,还有就是"abc,123"算作一个单词,而"abc, 123"算作两个单词,分别为"abc,"和"123",具体要求为:

        1. 当读取一个单词,当前行不超过80个字符(包括空格),就直接打印在后面(注意单词直接用空格隔开),否则就另起一行在开头处打印该单词,所有单词的最大长度为80;

        2. 当读取<br>时直接开启一新行;

        3. 当读取<hr>时,如果<hr>不在新行开头就另起一行连续打印80个'-'后再开启一新行;

        4. 全部翻译完后用一个空行结束;

题目链接

注释代码:

/*                      * Problem ID : POJ 2271 HTML * Author     : Lirx.t.Una                      * Language   : C            * Run Time   : 0 ms                      * Run Memory : 156 KB                     */ #include <string.h>#include <stdio.h>//maximum number of characters per line//每行单词的最大数量#defineMAXNCHPL80charw[MAXNCHPL + 1];//word,存放单词intmain() {intcnt;//count,计数当前行中字符的个数intwl;//word length,单词的长度inti;cnt = 0;while ( ~scanf("%s", w) ) {if ( !strcmp(w, "<hr>") ) {if ( cnt ) {putchar('\n');cnt = 0;//换行后新一行的字符数量清空}//若已经在新一行开始处则直接打印'-'i = MAXNCHPL;while ( i-- )putchar('-');putchar('\n');continue;}if ( !strcmp(w, "<br>") ) {putchar('\n');cnt = 0;continue;}wl = strlen(w);if ( !cnt ) {//如果在一个新行开始处printf("%s", w);//则直接打印该单词cnt = wl;//并将当前行的单词数置为单词长度continue;}if ( cnt + wl + 1 > MAXNCHPL ) {//如果已经在行中间位置,并且//加上当前单词后超出行限80个字符printf("\n%s", w);//则需另起一行在开始处打印cnt = wl;//同样需要跟新当前行字符数}else {printf(" %s", w);//否则就直接打印,注意和前一个单词中间空一格cnt += wl + 1;//注意累加字符数的时候需要把空格加上}}putchar('\n');//完成后打印一个空行作为结束标志return 0;}

无注释代码:

#include <string.h>#include <stdio.h>#defineMAXNCHPL80charw[MAXNCHPL];intmain() {intcnt;intwl;inti;cnt = 0;while ( ~scanf("%s", w) ) {if ( !strcmp(w, "<hr>") ) {if ( cnt ) {putchar('\n');cnt = 0;}i = MAXNCHPL;while ( i-- )putchar('-');putchar('\n');continue;}if ( !strcmp(w, "<br>") ) {putchar('\n');cnt = 0;continue;}wl = strlen(w);if ( !cnt ) {printf("%s", w);cnt = wl;continue;}if ( cnt + wl + 1 > MAXNCHPL ) {printf("\n%s", w);cnt = wl;}else {printf(" %s", w);cnt += wl + 1;}}putchar('\n');return 0;}

单词解释:

Macintosh:苹果的一款老式的计算机

Netscape:美国网景公司,也只网景浏览器

native:n, 本地,与生俱来的,内置的

linebreak:n, 换行符

horizontal:adj, 水平的

ruler:n, 划线,划线板,尺子

tabulator:n, 制表符

newline:n, 换行符

namely:adv, 也就是

0 0
原创粉丝点击