练习题(六)

练习题(六)

时间限制: 1 Sec 内存限制: 128 MB
[命题人:171360140]
题目描述
After Li Hua went to college, he found that each student has his or her own student number. This student number is composed of nine digits from 0 to 9. Now he wants to do his own school number statistics, statistics 0~9. The number of occurrences of these nine numbers
输入
Input one student number
输出
Output 0~9 in sequence. Each line includes a number and the number of occurrences. (The middle is separated by a colon.and the number of times is 0, no output)
样例输入

171360140

样例输出

0:2
1:3
3:1
4:1
6:1
7:1

翻译:
题目描述
李华上大学后,发现每个学生都有自己的学号。这个学号由0到9的九位数组成。现在他想做自己的学号统计,统计0~9。这九个数字的出现次数
输入
输入一个学号
输出
按顺序输出0~9。每行包括一个数字和出现次数。(中间用冒号分隔。次数为0,无输出)
样例输入

171360140

样例输出

0:2
1:3
3:1
4:1
6:1
7:1

来源/分类
寒假练习赛
题目网址: http://47.112.31.182/problem.php?id=1070
题意:
本题为水题,统计0-9出现的次数
思路:
这个题可以用map映照然后出现一次把后面映照的值加一
代码:

#include <iostream>
#include <string>  
#include <map>
using namespace std;
int main()
{
    map<char,int> m;    //创建一个map映照容器
    string s;
    cin>>s;
    for(int i=0;i<=9;i++)
    {
        m[i+'0']=0; //先把0-9的键值映照得值都为0
    }
    for(int i=0;i<9;i++)    //遍历学号
    {
        m[s[i]]++;  //对应的键值出现一次后面的值++
    }
    map<char,int>::iterator it; //定义迭代器
    for(it=m.begin();it!=m.end();it++)
        if((*it).second!=0) //次数不为0输出
            cout<<(*it).first<<":"<<(*it).second<<endl; //first对应的键值,second是键映照的值
    return 0;
}

运行结果:
在这里插入图片描述
总结:
练习一下map映照容器

猜你喜欢

转载自blog.csdn.net/qq_41657977/article/details/86701565