- 注意控制已有字符的输出
- 存放的输出了就减一,存放个数必须大于零
#include<iostream>
#include<vector>
#include<cctype>
#include<map>
#include<set>
#include<sstream>
#include<string>
#include<cstdio>
#include<algorithm>
const int maxn=6;
using namespace std;
int cnt[maxn];
int main() {
string s;
cin>>s;
for(int i=0; i<s.size(); i++) {
if(s[i]=='P') {
cnt[0]++;
} else if(s[i]=='A') {
cnt[1]++;
} else if(s[i]=='T') {
cnt[2]++;
} else if(s[i]=='e') {
cnt[3]++;
} else if(s[i]=='s') {
cnt[4]++;
} else if(s[i]=='t') {
cnt[5]++;
}
}
while(cnt[0]>0||cnt[1]>0||cnt[2]>0||cnt[3]>0||cnt[4]>0||cnt[5]>0) {
if(cnt[0]-->0) cout<<'P';
if(cnt[1]-->0) cout<<'A';
if(cnt[2]-->0) cout<<'T';
if(cnt[3]-->0) cout<<'e';
if(cnt[4]-->0) cout<<'s';
if(cnt[5]-->0) cout<<'t';
}
return 0;
}
emmm,柳诺小姐姐的代码
#include <iostream>
using namespace std;
int main() {
int map[128] = {0}, c;
while ((c = cin.get()) != EOF) map[c]++;
while (map['P'] > 0 || map['A'] > 0 || map['T'] > 0 || map['e'] > 0 || map['s'] > 0 || map['t'] > 0) {
if (map['P']-- > 0) cout << 'P';
if (map['A']-- > 0) cout << 'A';
if (map['T']-- > 0) cout << 'T';
if (map['e']-- > 0) cout << 'e';
if (map['s']-- > 0) cout << 's';
if (map['t']-- > 0) cout << 't';
}
return 0;
}