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

题目描述:
编写一个函数,计算字符串中含有的不同字符的个数。字符在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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章