我的PAT-BASIC代码仓:https://github.com/617076674/PAT-BASIC
原题链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805300404535296
题目描述:
知识点:字符串
思路:将输入的信息当作字符串读入,统计每个字符出现的个数
最后输出结果需要按字符数字的升序输出。
时间复杂度是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++解题报告: