题解 | #字符个数统计#

字符个数统计

https://www.nowcoder.com/practice/eb94f6a5b2ba49c6ac72d40b5ce95f50

#include <iostream>
using namespace std;

int main() {
    string s;
    cin>>s;
    int n[128] = {0};
    int res=0;
    int sum = 0;
    for(int i = 0; i < s.length(); i++) {
        res = (int)s[i];  //将字符强制转换成int
        n[res] = 1;       //用数组下标记录,并置1,下次再来相同的下标,一直是1
    }
    for(int j = 0; j < 128; j++) {
        if (n[j] == 1) {
            sum++;        //统计数组中值为1的个数
        }
    }
    cout << sum;
}

全部评论

相关推荐

allin实习的大白...:我把第二个项目发出来了,如果感兴趣可以去研究研究,欢迎交流。 https://gitee.com/jtyjtyjty333/ind-dist-ai-sec-edge-cloud https://github.com/jtylab/ind-dist-ai-sec-edge-cloud
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务