华为机试题:字符个数统计

来源:互联网 发布:淘宝主图视频上传过程 编辑:程序博客网 时间:2024/05/18 01:56

题目描述:
编写一个函数,计算字符串中含有的不同字符的个数。字符在ACSII码范围内(0~127)。不在范围内的不作统计。
输入描述:

输入N个字符,字符在ACSII码范围内。

输出描述:

输出范围在(0~127)字符的个数。

输入例子:

abc

输出例子:

3

#include <iostream>#include <string>using namespace std;int main()    {    string b;    getline(cin,b);    int count=0;    for(int i=0;i<=127;i++)        if(b.find(i)!=string::npos)        count++;    cout<<count;}
#include<iostream>#include<map>using namespace std;int main(){    map <char, int> s;    char c;    while (cin >> c){        s.insert({ c, 1 });    }    int n = 0;    for (auto d : s){        if (d.first >= 0 && d.first <= 127)            n++;    }    cout << n << endl;    return 0;}

方法同上

#include<iostream>#include<set>using namespace std;int main(){    char c;    set<char> s;    while(cin>>c){        if(c>=0 && c<=127){            s.insert(c);        }    }    cout << s.size() <<endl;}
0 0
原创粉丝点击