PAT 乙级1041
题目: 输出PATest
给定一个长度不超过10^4的仅由英文字母构成的字符串。请将字符重新调整顺序,按 PATestPATest....这样的顺序输出,并忽略其它字符。当然,六种字符的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按 PATest 的顺序打印,直到所有字符都被输出。
输入格式:
输入在一行中给出一个长度不超过 10^4的、仅由英文字母构成的非空字符串。
输出格式:
在一行中按题目要求输出排序后的字符串。题目保证输出非空。
思路:遍历字符串记录所题目对应的的字符在字符串中的个数,然后通过循环输出题目要求的字符
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str,s="PATest";
cin >> str;
int len = str.size();
int index[117] = { 0 };
for (int i = 0; i < len; i++)
{
if ((str[i] >= 'A' && str[i] <= 'T') || (str[i] >= 'a' && str[i] <= 't'))
{
int teamp = str[i];
index[teamp]=index[teamp]+1;//大致记录所有符合要求的字符的个数
}
}
int flag = 1;
while (flag)
{
flag = 0;
int count = 0;//计数
for (int i = 0; i < s.size(); i++)
{
int step = s[i];
if (index[step] != 0)//如果这个字符没有用完,那么就输出该字符
{
cout << s[i];
flag = 1;
index[step]--;//字符个数减一
}
else count++;//记录遍历中用完的字符个数
}
if (count == s.size())//如果所有字符都用完循环结束
flag = 0;
}
return 0;
}
4的、仅由英文字母构成的字符串。请将字符重新调整顺序,按
的、仅由英文字母构成的字符串。请将字符重新调整顺序,按 PATestPATest.... 这样的顺序输出,并忽略其它字符。当然,六种字符的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按 PATest 的顺序打印,直到所有字符都被输出。
浙公网安备 33010602011771号