`

POJ 2271 HTML

阅读更多
http://poj.org/problem?id=2271

题意:给你HTML代码,翻译出来
要点:
①每行长度不得超过80
②HTML标签只有2个
<br>:换行
<hr>:换行【若本来就在开头不用先换行】再输出80个'-',接着换行
③最后要换行

Sample Input
Hallo, dies ist eine
ziemlich lange Zeile, die in Html
aber nicht umgebrochen wird.
<br>
Zwei <br> <br> produzieren zwei Newlines.
Es gibt auch noch das tag <hr> was einen Trenner darstellt.
Zwei <hr> <hr> produzieren zwei Horizontal Rulers.
Achtung       mehrere Leerzeichen irritieren

Html genauso wenig wie

mehrere Leerzeilen.

Sample Output
Hallo, dies ist eine ziemlich lange Zeile, die in Html aber nicht umgebrochen
wird.
Zwei

produzieren zwei Newlines. Es gibt auch noch das tag
--------------------------------------------------------------------------------
was einen Trenner darstellt. Zwei
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
produzieren zwei Horizontal Rulers. Achtung mehrere Leerzeichen irritieren Html
genauso wenig wie mehrere Leerzeilen.


题目飞一般的水,只要你细心,会用打表调试,并且测试极端情况的正确性
如输入:

11111111112222222222333333333344444444445555555555666666666677777777778888888888
11111111112222222222333333333344444444445555555555666666666677777777778888888888
11111111112222222222333333333344444444445555555555666666666677777777778888888888


#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <utility>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h>
using namespace std;

int main()
{
	//fstream out ("d:\\ans.txt", ios::out);
	int num = 0;
	char word[85];
	while (scanf ("%s", word) != EOF)
	{
		if (!strcmp (word, "<br>"))
		{
			printf ("\n")/*, out << endl*/;
			num = 0;
			continue;
		}
		if (!strcmp (word, "<hr>"))
		{
			if (num > 0)
				printf ("\n")/*, out << endl*/;
			num = 0;
			printf ("--------------------------------------------------------------------------------\n");
			//out << "--------------------------------------------------------------------------------\n";
			continue;
		}
		int len = strlen(word);
		if (num == 0)
			num = len;
		else
		{
			if (num + 1 + len > 80)   //+1是因为空格
				printf ("\n"), num = len/*, out << endl*/;
			else printf (" "), num += 1 + len/*, out << ' '*/;
		}
		printf ("%s", word)/*, out << word*/;
	}
	printf ("\n");
	//out << endl;
	return 0;
}
7
4
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics