PAT b1043 输出PATest

PAT b1043 输出PATest

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

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

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

输入样例:

redlesPayBestPATTopTeePHPereatitAPPT

输出样例:

PATestPATestPTetPTePePee

难度不大的0.52一道题,我的思路分了四步写在注释里面了,写的算是很复杂的,还有很大的优化空间,不过很顺利地一次就过了,继续加油。
今晚去膜拜一下各路大神是怎么写的,学习一下。

#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>

using namespace std;

#define MaxSize 10000
//PAT 1043 输出PATest
// 1、筛选 "PATest"
// 2、对PATest六个字母分别计数
// 3、按照轮次输出,若某一个num_P[i] == 0,输出停止,其他照旧
// 4、直到六个num全部=0; 

bool PATword(char c){   //判断一个字母是否为PATest 
	if(c == 'P' || c == 'A' || c == 'T' || c == 'e' || c == 's' || c == 't'){
		return true;
	}
	
	return false;
}

int MAXPAT(int a[]){
	int max_number = a[0];
	for(int i=1;i<=5;i++){
		if(a[i]>max_number) max_number = a[i];
	}
	
	return max_number;
}

void ChoosePAT(char Ch[],char newCh[]){
	int i,j,k; 
	int length_1 = strlen(Ch);
	for(j=0,k=0;j<length_1;j++){
		if(PATword(Ch[j]) == true){
			newCh[k++] = Ch[j]; //k就是筛选过后的newCh长度 
		}
	}
}

int main(){
	char ch[MaxSize];
	char ch2[MaxSize];
	int length_ch2;
	cin>>ch;
	int PATnumber[6]={0,0,0,0,0,0};//初始PATest字母数量都是0 
	
	ChoosePAT(ch,ch2);
	length_ch2 = strlen(ch2);
	
	for(int i=0;i<length_ch2;i++){
		if(ch2[i] == 'P') PATnumber[0]++;
		if(ch2[i] == 'A') PATnumber[1]++;
		if(ch2[i] == 'T') PATnumber[2]++;
		if(ch2[i] == 'e') PATnumber[3]++;
		if(ch2[i] == 's') PATnumber[4]++;
		if(ch2[i] == 't') PATnumber[5]++;
	}
		
	int MaxPATnumber = MAXPAT(PATnumber);
	
	for(int m=0;m<MaxPATnumber;m++){
		if(PATnumber[0]>0) cout<<'P'; PATnumber[0]--;
		if(PATnumber[1]>0) cout<<'A'; PATnumber[1]--;
		if(PATnumber[2]>0) cout<<'T'; PATnumber[2]--;
		if(PATnumber[3]>0) cout<<'e'; PATnumber[3]--;
		if(PATnumber[4]>0) cout<<'s'; PATnumber[4]--;
		if(PATnumber[5]>0) cout<<'t'; PATnumber[5]--;
	}

	return 0;
} 

运行调试,结果正确
PAT b1043 输出PATest
PAT通过????
PAT b1043 输出PATest

2019.02.28 数据结构学习 PAT ZaneLing

(明天姥姥就要开课了,加油,3月!)

浙大等我。