题目:https://pintia.cn/problem-sets/994805260223102976/problems/994805280074743808

这题也很水,不过一开始我没设置times变量,用的hashtable[maxi],结果就是在输出的时候哈希表也在改变,最后少数出了几行;

代码

  1. #include<iostream>
  2. #include<vector>
  3. #include<cctype>
  4. #include<cstring>
  5. #include<string>
  6. #include<algorithm>
  7. using namespace std;
  8. int main(){
  9. string input;
  10. string example = "PATest";
  11. vector<int> hashtable(6,0);
  12. getline(cin, input);
  13. for(int i = 0; i < input.size(); i++){
  14. if(example.find(input[i])!=string::npos){
  15. hashtable[example.find(input[i])]++;
  16. }
  17. }
  18. //取hashtable最大值
  19. int maxi = 0;
  20. for(int i = 0; i < 6; i++){
  21. if(hashtable[i]>=hashtable[maxi]) maxi = i;
  22. }
  23. int times = hashtable[maxi];
  24. for(int i = 0; i < times; i++){
  25. for(int j = 0; j < 6; j++){
  26. if(hashtable[j]!=0){
  27. printf("%c",example[j]);
  28. hashtable[j]--;
  29. }
  30. }
  31. }
  32. }
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. int map[128] = {0}, c;
  5. while ((c = cin.get()) != EOF) map[c]++;
  6. while (map['P'] > 0 || map['A'] > 0 || map['T'] > 0 || map['e'] > 0 || map['s'] > 0 || map['t'] > 0) {
  7. if (map['P']-- > 0) cout << 'P';
  8. if (map['A']-- > 0) cout << 'A';
  9. if (map['T']-- > 0) cout << 'T';
  10. if (map['e']-- > 0) cout << 'e';
  11. if (map['s']-- > 0) cout << 's';
  12. if (map['t']-- > 0) cout << 't';
  13. }
  14. return 0;
  15. }