我的PAT-BASIC代码仓:https://github.com/617076674/PAT-BASIC

原题链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805280074743808

题目描述:

PAT-BASIC1043——输出PATest

知识点:计数

思路:分别统计字符‘P’、‘A’、‘T’、‘e’、‘s’、‘t’的数量,再依次输出即可

每输出一个字符,其对应的数量就需要减一,一旦数量为0,则不输出该字符。

C++代码:

#include<iostream>
#include<string>

using namespace std;

int main() {
	string input;
	getline(cin, input);

	int count[6];//count[0]统计P的数量,count[1]统计A的数量,count[2]统计T的数量,count[3]统计e的数量,count[4]统计s的数量,count[5]统计t的数量
	for (int i = 0; i < 6; i++) {
		count[i] = 0;
	}

	for (int i = 0; i < input.length(); i++) {
		if (input[i] == 'P') {
			count[0]++;
		} else if (input[i] == 'A') {
			count[1]++;
		} else if (input[i] == 'T') {
			count[2]++;
		} else if (input[i] == 'e') {
			count[3]++;
		} else if (input[i] == 's') {
			count[4]++;
		} else if (input[i] == 't') {
			count[5]++;
		}
	}

	bool flag;
	while (true) {
		flag = true;
		if (count[0]-- > 0) {
			cout << "P";
			flag = false;
		}
		if (count[1]-- > 0) {
			cout << "A";
			flag = false;
		}
		if (count[2]-- > 0) {
			cout << "T";
			flag = false;
		}
		if (count[3]-- > 0) {
			cout << "e";
			flag = false;
		}
		if (count[4]-- > 0) {
			cout << "s";
			flag = false;
		}
		if (count[5]-- > 0) {
			cout << "t";
			flag = false;
		}
		if (flag) {
			break;
		}
	}
}

C++解题报告:

PAT-BASIC1043——输出PATest

 

相关文章:

  • 2021-12-25
  • 2021-05-20
  • 2021-06-23
  • 2021-12-09
  • 2022-12-23
  • 2022-01-04
猜你喜欢
  • 2021-05-26
  • 2022-12-23
  • 2022-01-07
  • 2021-05-20
  • 2022-02-28
  • 2021-03-31
  • 2021-06-04
相关资源
相似解决方案