给定一个长度不超过 104 的、仅由英文字母构成的字符串。请将字符重新调整顺序,按 PATestPATest....
这样的顺序输出,并忽略其它字符。当然,六种字符的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按 PATest 的顺序打印,直到所有字符都被输出。
输入格式:
输入在一行中给出一个长度不超过 104 的、仅由英文字母构成的非空字符串。
输出格式:
在一行中按题目要求输出排序后的字符串。题目保证输出非空。
输入样例:
redlesPayBestPATTopTeePHPereatitAPPT
输出样例:
PATestPATestPTetPTePePee
//对于这道题目,可能有很多人想的是先循环一遍,看看有多少个需要的字符,然后在输出,这样很麻烦。我这个方法只要将字符串循环一遍就ok了, //然后在循环中按顺序用find()函数查找字符串中有没有我们需要的字符,并输出就可以了 #include<iostream> #include<vector> using namespace std; int main(){ string a;//保存输入的字符串 getline(cin,a); for(int i=0;i<a.length()/2+1;i++){//开始对字符串进行循环,因为一次输出好几个字符,所以直接将长度减半了 if(a.find('P')<a.length()){//如果能找到P就输出P,然就将那个找到的位置换成空格,后面也是这样依次输出 cout<<"P"; a[a.find('P')]=' '; } if(a.find('A')<a.length()){ cout<<"A"; a[a.find('A')]=' '; } if(a.find('T')<a.length()){ cout<<"T"; a[a.find('T')]=' '; } if(a.find('e')<a.length()){ cout<<"e"; a[a.find('e')]=' '; } if(a.find('s')<a.length()){ cout<<"s"; a[a.find('s')]=' '; } if(a.find('t')<a.length()){ cout<<"t"; a[a.find('t')]=' '; } } }