题目1032:ZOJ

题目描述:
读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当某个字符用完时,剩下的仍然按照ZOJ的顺序输出。
输入:
题目包含多组用例,每组用例占一行,包含ZOJ三个字符,当输入“E”时表示输入结束。
1<=length<=100。
输出:
对于每组输入,请输出一行,表示按照要求处理后的字符串。
具体可见样例。
样例输入:
ZZOOOJJJ
ZZZZOOOOOJJJ
ZOOOJJ
E
样例输出:
ZOJZOJOJ
ZOJZOJZOJZOO

ZOJOJO

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str;
	while(cin>>str&&str!="E")
	{
		int count[3]={0};
		int k=str.length();
		for(int i=0;i<k;i++)
		{
			if(str[i]=='Z')
				count[0]++;
			else if(str[i]=='O')
				count[1]++;
			else if(str[i]=='J')
				count[2]++;
		}
		int min=count[0],max=count[0];
		for(int i=1;i<3;i++)
		{
			if(min>count[i])min=count[i];
			else if(max<count[i])max=count[i];
		}
		for(int i=0;i<min;i++)
			cout<<"ZOJ";
		count[0]-=min;
		count[1]-=min;
		count[2]-=min;
		for(int i=0;i<max-min;i++)
		{
			if(count[0]-->0)cout<<'Z';
			if(count[1]-->0)cout<<'O';
			if(count[2]-->0)cout<<'J';
		}
		cout<<endl;

	}
	return 0;
}


极简,专注,速度,极致
原文地址:https://www.cnblogs.com/simplelifestyle/p/3761883.html