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


    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;
    }
    
  • 相关阅读:
    加关注
    UI设计
    敏捷开发
    java书箱
    怎么优化JAVA程序的执行效率和性能?
    sql访注入
    Matlab中plot函数全功能解析
    matlab分割背景与物体
    ssh免密码登录
    c++字符串详解(转)
  • 原文地址:https://www.cnblogs.com/bingers/p/13095549.html
Copyright © 2020-2023  润新知