题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1088
简单模拟,多注意细节就行了。
代码如下:有注释
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/
char xh[201314][88];
int main()
{
int i,j,len,n;
n=0;
while(scanf("%s",xh[n])!=-1)
n++;
len=0;//标记现在在这一行的第几个字节
for(i=0;i<n;i++)
{
int st=strlen(xh[i]);//xh[i]的长度
if(strcmp(xh[i],"<hr>")==0)
{
if(len!=0)//如果len不在第一个,那么换行
printf("\n");
for(j=0;j<80;j++)
printf("-");
printf("\n");
len=0;
}
else if(strcmp(xh[i],"<br>")==0)
{
printf("\n");
len=0;
}
else
{
if(len+1+st>80)//len+空格+单词长度>80,换行
{
printf("\n%s",xh[i]);
len=st;
}
else if(len!=0)//len不在第一个
{
printf(" %s",xh[i]);
len+=st+1;
}
else//len在第一个
{
printf("%s",xh[i]);
len=st;
}
}
}
printf("\n");
return 520;
}