HDU - 1088 Write a simple HTML Browser
问题描述:
If you ever tried toread a html document on a Macintosh, you know how hard it is if no Netscape is installed. Now, who can forget to install a HTML browser? This is very easy because most of the times you don't need one on a MAC because there is a Acrobate Reader which is native to MAC. But if you ever need one, what do you do? Your task is to write a small html-browser. It should only display the content of the input-file and knows only the html commands (tags)which is a linebreak and
which is a horizontal ruler. Then you should treat all tabulators, spaces and newlines as one space and display the resulting text with no more than 80 characters on a line.
输入说明:
The input consists of a text you should display. This text consists of words and HTML tags separated by one or more spaces, tabulators or newlines.
A word is a sequence of letters, numbers and punctuation. For example, “abc,123” is one word, but “abc, 123” are two words, namely “abc,” and “123”. A word is always shorter than 81 characters and does not contain any ‘<’ or ‘>’. All HTML tags are either
or
.
输出说明:
You should display the the resulting text using this rules:
. If you read a word in the input and the resulting line does not get longer than 80 chars, print it, else print it on a new line.
. If you read a
in the input, start a new line.
. If you read a
in the input, start a new line unless you already are at the beginning of a line, display 80 characters of ‘-’ and start a new line (again).
The last line is ended by a newline character.
思路:
按照题目的要求根据输入输出一些语句,难度不是特别高,但是比较烦,输入需要一行行的读,“< b r>”和“< h r >”两个特殊字符按照要求,先进行判断,方法和上一题一样,用strcmp函数进行判断,输出也是按照题意,直接输出,注意每到80个字符就要换行,这个用一个cont计数就可以了。
p.s 注意输出‘-’的个数,可以直接复制输出范例中的
AC代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
char s[1100];
int i,j,k,l,m,n;
int cnt=0;
while(~scanf("%s",s))
{
if(!strcmp(s,"<br>"))
{
cnt=0;
printf("\n");
}
else if(!strcmp(s,"<hr>"))
{
if(cnt)
printf("\n--------------------------------------------------------------------------------\n");
else
printf("--------------------------------------------------------------------------------\n");
cnt=0;
}
else
{
l=strlen(s);
if(!cnt)
{
cnt=l;
printf("%s",s);
}
else if(cnt+l+1>80)
{
cnt=l;
printf("\n%s",s);
}
else
{
cnt+=l+1;
printf(" %s",s);
}
}
}
printf("\n");
return 0;
}