0
Follow
0
View

A c++ problem with arrays

dashan0098 注册会员
2023-02-26 18:21

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.

du7775 注册会员
2023-02-26 18:21

I just into the pit of small white, do not spray

About the Author

Question Info

Publish Time
2023-02-26 18:21
Update Time
2023-02-26 18:21

Related Question

如何用h3c详细解答留的任务看看有没有人可可以解答?

关于#java#的问题:Dao Service Controller层 前端代码如下,为什么页面显示的是十六进制数据

pyecharts制作折线图时,对x轴的修改无效

请求AddAngleBetweenVectorsCost

如何找到数组c++的所有元素的总和

不能不队-无理-socket编程

我如何使它使java停止转换我的char(s)到其他数值

如何用c语言实现blas库中dger函数功能

echarts 自己画svg

mcms无法加载启动类