PAT Basic 1043. 输出PATest

发布时间 2023-03-22 21:09:25作者: 十豆加日月

PAT Basic 1043. 输出PATest

1. 题目描述:

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

2. 输入格式:

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

3. 输出格式:

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

4. 输入样例:

redlesPayBestPATTopTeePHPereatitAPPT

5. 输出样例:

PATestPATestPTetPTePePee

6. 性能要求:

Code Size Limit
16 KB
Time Limit
400 ms
Memory Limit
64 MB

思路:

除草题,考察基础IO,统计"PATest"各字符的数量即可。

写的有点冗余了,比较简洁的代码参见PAT-Basic-1043. 输出PATest – Lnyan's Blog (llonely.com)

My Code:

#include <stdio.h>

int main(void)
{
    char words[10001] = "";
    int count[6] = {0}; // count the number of PATest
    char *pChar = NULL;
    int minNum=0, maxNum=0;
    int i=0;
    
    scanf("%s", words);
    pChar = words;
    
    while(*pChar)
    {
        switch(*pChar)
        {
            case 'P':
                ++count[0];
                break;
            case 'A':
                ++count[1];
                break;
            case 'T':
                ++count[2];
                break;
            case 'e':
                ++count[3];
                break;
            case 's':
                ++count[4];
                break;
            case 't':
                ++count[5];
                break;
            default:
                break;
        }
        
        ++pChar;
    }
    
    maxNum = minNum = count[0]; // the number of 'P'
    for(i=1; i<6; i++)
    {
        if(count[i]>maxNum)
            maxNum = count[i];
        if(count[i]<minNum)
            minNum = count[i];
    }
    
    for(i=0; i<6; i++)
    {
        count[i] -= minNum;
    }
    
    for(i=0; i<minNum; i++)
    {
        printf("PATest");
    }
    
    for(i=0; i<maxNum-minNum; i++)
    {
        if(count[0])
        {
            --count[0];
            printf("P");
        }
        if(count[1])
        {
            --count[1];
            printf("A");
        }
        if(count[2])
        {
            --count[2];
            printf("T");
        }
        if(count[3])
        {
            --count[3];
            printf("e");
        }
        if(count[4])
        {
            --count[4];
            printf("s");
        }
        if(count[5])
        {
            --count[5];
            printf("t");
        }
    }
    
    printf("\n");
    
    return 0;
}