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

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

题目描述:

PAT-BASIC1021——个位数统计

知识点:字符串

思路:将输入的信息当作字符串读入,统计每个字符出现的个数

最后输出结果需要按字符数字的升序输出。

时间复杂度是O(n),其中n为输入的数字的位数。空间复杂度是O(1)。

C++代码:

#include<iostream>
#include<string>

using namespace std;

int main() {
	string input;
	cin >> input;
	int count[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
	for (int i = 0; i < input.length(); i++) {
		for (int j = 0; j <= 9; j++) {
			if (input[i] - '0' == j) {
				count[j]++;
				break;
			}
		}
	}
	for (int i = 0; i <= 9; i++) {
		if (count[i] == 0) {
			continue;
		}
		cout << i << ":" << count[i] << endl;
	}
}

C++解题报告:

PAT-BASIC1021——个位数统计

 

相关文章:

  • 2022-01-23
  • 2021-09-04
  • 2022-12-23
  • 2021-04-04
  • 2021-12-21
  • 2021-07-29
  • 2022-12-23
猜你喜欢
  • 2021-06-08
  • 2022-12-23
  • 2021-07-04
  • 2021-07-15
  • 2021-07-08
  • 2022-02-20
  • 2022-12-23
相关资源
相似解决方案