hdu3783&&九度1032 ZOJ(字符串或栈)

ZOJ

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2086    Accepted Submission(s): 1440


Problem Description
读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当某个字符用完时,剩下的仍然按照ZOJ的顺序输出。
 

Input
题目包含多组用例,每组用例占一行,包含ZOJ三个字符,当输入“E”时表示输入结束。
1<=length<=100。
 

Output
对于每组输入,请输出一行,表示按照要求处理后的字符串。
具体可见样例。
 

Sample Input
  
  
ZZOOOJJJ ZZZZOOOOOJJJ ZOOOJJ E
 

Sample Output
  
  
ZOJZOJOJ ZOJZOJZOJZOO ZOJOJO
 

Source

浙大计算机研究生复试上机考试-2009年  



//自己的思路是使用三个栈,分别放字符Z,O,J,然后再按Z,O,J的顺序出栈即可。

#include<stdio.h>
#include<string.h>
#include<stack>
#include<algorithm>
using namespace std;
char str[105];
stack<char>s1;
stack<char>s2;
stack<char>s3;
int main()
{
	while(scanf("%s",str)!=EOF)
	{
		int len=strlen(str);
		while(!s1.empty())
		{
			s1.pop();
		}
		while(!s2.empty())
		{
			s2.pop();
		}
		while(!s3.empty())
		{
			s3.pop();
		}
		if(str[0]=='E')
		{
			break;
		}
		else
		{
			for(int i=0;i<len;i++)
			{
				if(str[i]=='Z')
				{
					s1.push(str[i]);
				}
				else if(str[i]=='O')
				{
					s2.push(str[i]);
				}
				else if(str[i]=='J')
				{
					s3.push(str[i]);
				}
			}
		}
		while(!s1.empty()||!s2.empty()||!s3.empty())
		{
			/**
			char ch1=s1.top();
			printf("%c",ch1);
			s1.pop();
			char ch2=s2.top();
			printf("%c",ch2);
			s2.pop();
			char ch3=s3.top();
			printf("%c",ch3);
			s3.pop();
			*/
			if(!s1.empty())
			{
				char ch1=s1.top();
				printf("%c",ch1);
				s1.pop();
			}
			if(!s2.empty())
			{
				char ch2=s2.top();
				printf("%c",ch2);
				s2.pop();
			}
			if(!s3.empty())
			{
				char ch3=s3.top();
				printf("%c",ch3);
				s3.pop();
			}
		}
		printf("\n");
	}
	return 0;
}
</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp">//下面是别人的更好一点的思路,不用栈
</pre><pre name="code" class="cpp">
<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
char str[105];
int main()
{
	int z,o,j;
	int len;
	while(scanf("%s",str)!=EOF)
	{
		z=o=j=0;
		len=strlen(str);
		if(str[0]=='E')
		{
			break;
		}
		else
		{
			for(int i=0;i<len;i++)
			{
				if(str[i]=='Z')
				z++;
				else if(str[i]=='O')
				o++;
				else if(str[i]=='J')
				j++;
			}
			while(z||o||j)
			{
				if(z)
				{
					printf("Z");
					z--;
				}
				if(o)
				{
					printf("O");
					o--;
				}
				if(j)
				{
					printf("J");
					j--;
				}
			}
		}
		printf("\n");
	}
	return 0;
}


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值