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

被折叠的 条评论
为什么被折叠?



