首页 > 其他 > 详细

PAT散列题---1043 输出PATest (20分)

时间:2020-06-11 20:12:24      阅读:36      评论:0      收藏:0      [点我收藏+]

1043 输出PATest (20分)

  • 注意控制已有字符的输出
  • 存放的输出了就减一,存放个数必须大于零
#include<iostream>
#include<vector>
#include<cctype>
#include<map>
#include<set>
#include<sstream>
#include<string>
#include<cstdio>
#include<algorithm>

const int maxn=6;
using namespace std;

int cnt[maxn];
int main() {

	string s;
	cin>>s;
	for(int i=0; i<s.size(); i++) {
		if(s[i]==‘P‘) {
			cnt[0]++;
		} else if(s[i]==‘A‘) {
			cnt[1]++;
		} else if(s[i]==‘T‘) {
			cnt[2]++;
		} else if(s[i]==‘e‘) {
			cnt[3]++;
		} else if(s[i]==‘s‘) {
			cnt[4]++;
		} else if(s[i]==‘t‘) {
			cnt[5]++;
		}
	}
	while(cnt[0]>0||cnt[1]>0||cnt[2]>0||cnt[3]>0||cnt[4]>0||cnt[5]>0) {
		if(cnt[0]-->0) cout<<‘P‘;
		if(cnt[1]-->0) cout<<‘A‘;
		if(cnt[2]-->0) cout<<‘T‘;
		if(cnt[3]-->0) cout<<‘e‘;
		if(cnt[4]-->0) cout<<‘s‘;
		if(cnt[5]-->0) cout<<‘t‘;
	}
	return 0;
}

emmm,柳诺小姐姐的代码

#include <iostream>
using namespace std;
int main() {
	int map[128] = {0}, c;
	while ((c = cin.get()) != EOF) map[c]++;
	while (map[‘P‘] > 0 || map[‘A‘] > 0 || map[‘T‘] > 0 || map[‘e‘] > 0 || map[‘s‘] > 0 || map[‘t‘] > 0) {
		if (map[‘P‘]-- > 0) cout << ‘P‘;
		if (map[‘A‘]-- > 0) cout << ‘A‘;
		if (map[‘T‘]-- > 0) cout << ‘T‘;
		if (map[‘e‘]-- > 0) cout << ‘e‘;
		if (map[‘s‘]-- > 0) cout << ‘s‘;
		if (map[‘t‘]-- > 0) cout << ‘t‘;
	}
	return 0;
}

PAT散列题---1043 输出PATest (20分)

原文:https://www.cnblogs.com/bingers/p/13095549.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!