努力ing
你浪费的今天是昨天死去的人所渴望的明天!!!

http://acm.hdu.edu.cn/showproblem.php?pid=1088

字符串问题

第一种方法

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str;
	int i,num=0;
	while(cin>>str)
	{
		if(str=="<br>")
		{
			cout<<endl;
			num=0;
			continue;
		}
		if(str=="<hr>")
		{
			if(num!=0) cout<<endl;
			for(i=0;i<80;i++)
				cout<<'-';
			cout<<endl;
			num=0;
			continue;
		}
		int t=str.length();
		if(num+1+t>80)
		{
			cout<<endl;
			cout<<str;
			num=t;
		}
		else {
			if(num!=0) cout<<' ';
			cout<<str;
			num+=1+t;
		}
	}
	cout<<endl;
	return 0;
}

 第二种方法:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str;
	char str1[10008];
	int i=0,j,num=0;
	while(cin>>str)
	{
		if(str=="<br>")
		{
			str1[i++]='\n';
			num=0;
			continue;
		}
		if(str=="<hr>")
		{
			if(num!=0) str1[i++]='\n';
			for(j=0;j<80;j++)
				str1[i++]='-';
			str1[i++]='\n';
			num=0;
			continue;
		}
		int t=str.length();
		if(num+1+t>80)
		{
			str1[i++]='\n';
			for(j=0;j<t;j++)
				str1[i++]=str[j];
			num=t;
		}
		else{
			if(num!=0) str1[i++]=' ';
			for(j=0;j<t;j++)
				str1[i++]=str[j];
			num+=1+t;
		}
	}
	str1[i]=0;
	cout<<str1<<endl;
	return 0;
}

 

posted on 2013-05-30 20:40  努力ing  阅读(204)  评论(0)    收藏  举报