傳送門
https://pintia.cn/problem-sets/994805260223102976/problems/994805280074743808
題目
給定一個(gè)長(zhǎng)度不超過10000的、僅由英文字母構(gòu)成的字符串。請(qǐng)將字符重新調(diào)整順序崭倘,按“PATestPATest....”這樣的順序輸出怀喉,并忽略其它字符亿汞。當(dāng)然徘跪,六種字符的個(gè)數(shù)不一定是一樣多的讳推,若某種字符已經(jīng)輸出完艾船,則余下的字符仍按PATest的順序打印葵腹,直到所有字符都被輸出。
輸入格式:
輸入在一行中給出一個(gè)長(zhǎng)度不超過10000的屿岂、僅由英文字母構(gòu)成的非空字符串践宴。
輸出格式:
在一行中按題目要求輸出排序后的字符串。題目保證輸出非空爷怀。
輸入樣例:
redlesPayBestPATTopTeePHPereatitAPPT
輸出樣例:
PATestPATestPTetPTePePee
分析
用6個(gè)變量記錄PATest的出現(xiàn)次數(shù)阻肩,然后按照從P到t的順序輸出,每輸出一次把計(jì)數(shù)器自減1运授,最終全為0時(shí)烤惊,停止輸出即可乔煞。
源代碼
//C/C++實(shí)現(xiàn)
#include <iostream>
#include <string.h>
using namespace std;
int main(){
char c[10001];
gets(c);
int countP = 0, countA = 0, countT = 0, counte = 0, counts = 0, countt = 0;
for(int i = 0; i < strlen(c); ++i){
if(c[i] == 'P'){
++countP;
}
else if(c[i] == 'A'){
++countA;
}
else if(c[i] == 'T'){
++countT;
}
else if(c[i] == 'e'){
++counte;
}
else if(c[i] == 's'){
++counts;
}
else if(c[i] == 't'){
++countt;
}
}
while(countP != 0 || countA !=0 || countT !=0 || counte != 0 || counts !=0 || countt !=0){
if(countP != 0){
printf("P");
--countP;
}
if(countA != 0){
printf("A");
--countA;
}
if(countT != 0){
printf("T");
--countT;
}
if(counte != 0){
printf("e");
--counte;
}
if(counts != 0){
printf("s");
--counts;
}
if(countt != 0){
printf("t");
--countt;
}
}
printf("\n");
return 0;
}
//Java實(shí)現(xiàn)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
char[] c = s.toCharArray();
boolean flag = true;
int cP = 0,cA = 0,cT = 0,ce = 0,cs = 0,ct = 0;
if(s.length() >=1 && s.length() <=10000){
for(int i=0;i<s.length();i++){
if(c[i] >= 65 && c[i] <= 90 || c[i] >=97 && c[i] <=122){
if(c[i] == 'P'){
cP ++;
}
else if(c[i] == 'A'){
cA ++;
}
else if(c[i] =='T'){
cT ++;
}
else if(c[i] =='e'){
ce ++;
}
else if(c[i] =='s'){
cs ++;
}
else if(c[i] == 't'){
ct ++;
}
continue;
}
else{
flag = false;
break;
}
}
if(flag) {
while(cP != 0 || cA !=0 || cT !=0 || ce != 0 || cs !=0 || ct !=0){
if(cP != 0){
System.out.print('P');
cP --;
}
if(cA != 0){
System.out.print('A');
cA --;
}
if(cT != 0){
System.out.print('T');
cT --;
}
if(ce != 0){
System.out.print('e');
ce --;
}
if(cs != 0){
System.out.print('s');
cs --;
}
if(ct != 0){
System.out.print('t');
ct --;
}
}
System.out.println();
}
}
}
}