華爲機試題:字符個數統計

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