zoj3168------------------------Sort ZOJ7

该博客介绍了一道编程题,要求对包含Z, O, J, 7和其他字符的字符串进行排序,使得Z在前,然后是O, J, 7,保持其他字符原有顺序。示例输入和输出分别给出了一个测试用例。" 118095985,7800548,使用蒙特卡罗方法计算π与解决复杂问题,"['机器学习', '数据挖掘', '深度学习', '神经网络']

Given a string of no more than 1000 characters. You are supposed to sort the characters into a substring of all Z's followed by O's, J's, 7's, and the rest of the other characters.

Input

Each case gives a string described by the problem. The string given contains no space.

Output

For each test case, output the resulting string. Note the order of the characters other than Z, O, J and 7 must be kept after sorting.

Sample Input

t7ZJ7OhO7B7O7irZtOhZdayJ77

Sample Output

ZZZOOOOJJ7777777thBirthday



每到‘Z’ ‘O’ ‘J’ ‘7’时,计数并在最后输出

其余传入另一数组,并在最后输出


代码:

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
	int o,j,s,i,k;
	string a;
	while(cin>>a)
	{
		char b[1000]={0};    //每次循环之前数组都置零 
		o=j=s=i=k=0;
		for(int i=0;i<a.size();i++) //a.size()是  string a 的长度 
		{
		if(a[i]=='Z')
			cout<<'Z';
		else if(a[i]=='O')	
			o++;
		else if(a[i]=='J')
			j++;
		else if(a[i]=='7')
			s++;
		else
		{
			b[k]=a[i];     //把其他字母传递入数组 b[k]并在最后直接输出 
			k++;
		}
		}
	while(o--)
		cout<<'O';
	while(j--)
		cout<<'J';
	while(s--)
		cout<<'7';
	cout<<b<<endl;
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值