列表

详情


CPP50. 统计字符串中各字母字符对应的个数

描述

键盘输入一个字符串,统计字符串中各个字母字符的个数。例如:键盘输入"Hello World!",上述字符串中各个字母字符的出现的次数为:
H:1
e:1
l:3
o:2
W:1
r:1
d:1
要求使用map实现,键的排序使用map默认排序即可。

输入描述

键盘输入任意一个字符串

输出描述

输出字母字符和字符对应的出现字数(注:相同字母的大小写算两个不同字符,字母字符和出现次数之间用:隔开,按 map 的默认顺序输出即可)

示例1

输入:

Hello World

输出:

H:1
W:1
d:1
e:1
l:3
o:2
r:1

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 2ms, 内存消耗: 388KB, 提交时间: 2022-01-02

#include <iostream>
#include <map>
#include <iterator>
// write your code here......

using namespace std;

int main() {

    char str[100] = { 0 };
    cin.getline(str, sizeof(str));

    // write your code here......
    map<char, int> m;
    for(int i = 0; str[i] != '\0'; i++){
        if(isalpha(str[i])){
        auto it = m.find(str[i]);
        if(it == m.end()){
            m.insert({str[i],1});
        }
        else{
            it->second++;
        }
        }
        
    }
    
    for(auto iter : m){
        cout<<iter.first<<":"<<iter.second<<endl;
    }
    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 396KB, 提交时间: 2021-12-12

#include <iostream>
// write your code here......
#include <map>
using namespace std;

int main() {

    char str[100] = { 0 };
    cin.getline(str, sizeof(str));

    // write your code here......
    map<char, int> m;
    for(int i = 0; str[i] != '\0'; i++){
        if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')){
            auto iter = m.find(str[i]);
            int val = iter == m.end() ? 0 : iter->second;
            m[str[i]] = ++val;
        }
    }
    
    for(auto iter = m.begin(); iter != m.end(); iter++){
        cout << iter->first << ":" << iter->second << endl;
    }

    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 396KB, 提交时间: 2021-11-18

#include <iostream>
// write your code here......
#include <map>
using namespace std;

int main()
{

    char str[100] = { 0 };
    cin.getline(str, sizeof(str));

    // write your code here......
    map<char,int>m;
    m.clear();
    for(int i=0; str[i]; i++)
    {
        if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z')) m[str[i]]++;
    }
    map<char,int>::iterator it;
    for(it=m.begin();it!=m.end();it++)
        cout<<it->first<<":"<<it->second<<endl;
    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 396KB, 提交时间: 2021-10-31

#include <iostream>
// write your code here......
#include <map>
#include <cctype>
using namespace std;

int main() {

    char str[100] = { 0 };
    cin.getline(str, sizeof(str));

    // write your code here......
    map<char, int> ch_i_map;
    for(char *p=str,ch; (ch = *p++); )
        if( isalpha(ch) )
            ++ch_i_map[ch];
    
    for(auto [ch, cnt] :  ch_i_map)
        cout << ch << ':' << cnt << endl;

    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 412KB, 提交时间: 2022-01-01

#include <iostream>
// write your code here......
#include <map>
#include <set>
#include <cstring>
using namespace std;

int main() {

    char str[100] = { 0 };
    cin.getline(str, sizeof(str));

    // write your code here......
    map<char,int> m;
    int len= strlen(str);
    map<char,int>::iterator iterator;
    for(int i=0;i<len;i++)
    {
        if((str[i]>='a'&&str[i]<='z') ||(str[i]>='A'&&str[i]<='Z') ) 
        {
            iterator=m.find(str[i]);
            if(iterator==m.end())
            {
                m[str[i]]=1;
            }
            else{
                int temp=m[str[i]];
                m[str[i]]=++temp;
            }      
        }   
     }
     for(map<char,int>::iterator iterator=m.begin();iterator!=m.end();iterator++)
     {
         cout<<iterator->first<<":"<<iterator->second<<endl;
     }
    
    return 0;
}

上一题