给定一个长度不超过 1 的、仅由英文字母构成的字符串。请将字符重新调整顺序,按 PATestPATest....
这样的顺序输出,并忽略其它字符。当然,六种字符的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按 PATest 的顺序打印,直到所有字符都被输出。
输入在一行中给出一个长度不超过 1 的、仅由英文字母构成的非空字符串。
在一行中按题目要求输出排序后的字符串。题目保证输出非空。
redlesPayBestPATTopTeePHPereatitAPPT
PATestPATestPTetPTePePee
代码如下:
#include<cstdio> #include<cstring> #include<string> #include<iostream> using namespace std; string str; int hashtable[7]={0}; int main() { getline(cin, str); for (int i = 0; i < str.length(); i++) { if (str[i] == ‘P‘) { hashtable[0]++; } else if (str[i] == ‘A‘) { hashtable[1]++; } else if (str[i] == ‘T‘) { hashtable[2]++; } else if (str[i] == ‘e‘) { hashtable[3]++; } else if (str[i] == ‘s‘) { hashtable[4]++; } else if (str[i] == ‘t‘) { hashtable[5]++; } } int m=0; for (int i = 0; i < 6; i++) { if(hashtable[i]>m){ m=hashtable[i];//循环次数为PATest中最多的字符数 }; } for (int j = 0; j <m; j++) { for (int i = 0; i < 6; i++) { if (hashtable[i] != 0) { if (i == 0) { printf("P"); hashtable[i]--; } else if (i == 1) { printf("A"); hashtable[i]--; } else if (i == 2) { printf("T"); hashtable[i]--; } else if (i == 3) { printf("e"); hashtable[i]--; } else if (i == 4) { printf("s"); hashtable[i]--; } else if (i == 5) { printf("t"); hashtable[i]--; } } } } printf("\n"); return 0; }
原文:https://www.cnblogs.com/migang/p/14781812.html