the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > according to the subject description, need to statistics the number of occurrences of each character in the character of the input, output and the minimum number of characters appears the number of times.
Here is sample code for the implementation using arrays:
#include
#include
using namespace std;
const int MAXN = 256; // 字符的种类数,ASCII码共有256个字符
int main() {
char ch;
int cnt[MAXN]; // 记录每个字符出现的次数
memset(cnt, 0, sizeof(cnt)); // 初始化为0
// 统计每个字符出现的次数
while (cin >> ch && ch != '*') {
cnt[ch]++;
}
// 找出出现次数最小的字符出现的次数
int minCnt = 1e9; // 初始化一个很大的数,确保能够找到更小的数
for (int i = 0; i < MAXN; i++) {
if (cnt[i] > 0 && cnt[i] < minCnt) {
minCnt = cnt[i];
}
}
// 输出结果
cout << minCnt << endl;
return 0;
}
The main idea is to define an array cnt with a size of 256. For each character input, the counter at the corresponding position is increased by 1. Finally, the array is scanned again to find the value of the counter that appears least.