7-13 统计工龄(20 分)
给定公司N名员工的工龄,要求按工龄增序输出每个工龄段有多少员工。
输入格式:
输入首先给出正整数N(≤105),即员工总人数;随后给出N个整数,即每个员工的工龄,范围在[0, 50]。
输出格式:
按工龄的递增顺序输出每个工龄的员工个数,格式为:“工龄:人数”。每项占一行。如果人数为0则不输出该项。
输入样例:
8
10 2 0 5 7 2 5 2
输出样例:
0:1
2:3
5:2
7:1
10:1
思路:
我们用map标记。因为map<key,value>里,是对key值自动排序的,正符合我们题目的要求。
利用迭代器遍历容器,输出即可。
————————————————————————————————————————————
#include<bits/stdc++.h>
using namespace std;
const int MAX = 1e5+10;
const int INF = 0x3fffffff;
int main(){
int n;
while(scanf("%d",&n)!=EOF){
map<int,int>mp;
int x;
for(int i=0;i<n;i++){
scanf("%d",&x);
mp[x]++;
}
map<int,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++){
printf("%d:%d\n",(*it).first,(*it).second);
}
}
return 0;
}