您的位置:首页 > 编程语言 > C语言/C++

PAT Basic 1043. 输出PATest(20)(C语言实现)

2017-05-18 23:46 363 查看

题目

给定一个长度不超过10000的、仅由英文字母构成的字符串。请将字符重新调整顺序,按“PATestPATest....”这样的顺序输出,并忽略其它字符。当然,六种字符的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按PATest的顺序打印,直到所有字符都被输出。

输入格式:

输入在一行中给出一个长度不超过10000的、仅由英文字母构成的非空字符串。

输出格式:

在一行中按题目要求输出排序后的字符串。题目保证输出非空。

输入样例:
redlesPayBestPATTopTeePHPereatitAPPT
输出样例:
PATestPATestPTetPTePePee

思路

记录次数的时候依然使用int[128]的数组,

这里不用管读到的是不是“PATest”里的字符,全记下来就好了,去判断反而浪费时间,

输出就看个人习惯了,我先找到出现最多的次数max,循环max次,每次输出都把6个的计数减1,计数减到0或负的就不输出了。

代码

最新代码@github,欢迎交流 ^_^

#include <stdio.h>
int main()
{
char c;
int count[128] = {0};       /* for each ASCII char */
int str[] = "PATest";       /* use as index for count[] */

/* read any char and count their numbers */
while((c = getchar()) != '\n')
count[(int)c]++;

/* among "PATest" find the most frequent char and asign its count to max */
int max = 0;
for(int i = 0; i < 6; i++)
if(max < count[str[i]])
max = count[str[i]];

/* before "max" prints, print any char in "PATest" if it is still left */
while(max--)
for(int j = 0; j < 6; j++)
if(count[str[j]]-- > 0)
putchar(str[j]);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: