PAT Class B-1043. Output PATest(20)-detailed + code

Subject: PAT Class B-1043. Output PATest(20)

Given a string consisting of only English letters of length 10000 or less. Please reorder the characters and output them in the order of "PATestPATest...." and ignore other characters. Of course, the number of six characters is not necessarily the same. If a certain character has been output, the remaining characters are still printed in the order of PATest until all characters are output.

Input format:

Input gives a non-empty string of only English letters of length 10000 or less in one line.

Output format:

Output the sorted string as required by the title in one line. The title guarantees that the output is not empty.

Input sample:
redlesPayBestPATTopTeePHPereatitAPPT
Sample output:
PATestPATestPTetPTePePee

Ideas:

    The first feeling when I see the title is a simple traversal output problem, first traverse it to count the number of 'P', 'A', 'T', 'e', ​​'s', 't', and then calculate them From the large "while loop" where the sum is not zero, if "PATest" outputs the non-zero letters in sequence, note that each is an "if" instead of an "else if".


#include<stdio.h>
#define N 10001
intmain()
{
	char a[N];
	int P=0, A=0, T=0, e=0, s=0, t=0,sum=0;
	int i = 0;
	scanf("%s", a);
	while (a[i])
	{
		if (a[i] == 'P')
		{
			P++;
		}
		else if (a[i] == 'A')
		{
			A++;
		}
		else if (a[i] == 'T')
		{
			T++;
		}
		else if (a[i] == 'e')
		{
			e++;
		}
		else if (a[i] == 's')
		{
			s++;
		}
		else if (a[i] == 't')
		{
			t++;
		}
		i++;
	}
	sum = P + A + T + e + s + t;//Total number
	while (sum != 0)
	{
		if (P != 0)
		{
			printf("P");
			P--;
		}
		if (A != 0)
		{
			printf("A");
			A--;
		}
		if (T != 0)
		{
			printf("T");
			T--;
		}
		if (e != 0)
		{
			printf("e");
			e--;
		}
		if (s != 0)
		{
			printf("s");
			s--;
		}
		if (t != 0)
		{
			printf("t");
			t--;
		}
		sum--;
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324528113&siteId=291194637