1021 个位数统计 (15 分)

最多(1000)位,显然要用字符串存储辣。

map<char,int> mp;
string s;

int main()
{
    cin>>s;

    for(int i=0;i<s.size();i++) mp[s[i]]++;

    for(auto t:mp)
        cout<<t.fi<<':'<<t.se<<endl;
   //system("pause");
    return 0;
}

换成数组:

int cnt[15];

int main()
{
   string s;
   cin>>s;

   for(int i=0;i<s.size();i++)
        cnt[s[i]-'0']++;

   for(int i=0;i<10;i++)
        if(cnt[i])
            cout<<i<<':'<<cnt[i]<<endl;
   //system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/fxh0707/p/14399845.html