统计工龄 (20分)(map一招搞定)

给定公司N名员工的工龄,要求按工龄增序输出每个工龄段有多少员工。

输入格式:
输入首先给出正整数N(≤10​5),即员工总人数;随后给出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中键值本身就是从小到大排序,直接把排序搞定了,不过这数据结构练习,我都用c++写,期末考试咋整。。。。
#include <iostream> 
#include <algorithm>
#include <map>
using namespace std;

int main(){
    
    
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	map<int,int> m;
	int n,age;
	cin >> n;
	for(int i = 0;i<n;i++){
    
    
		cin >> age;
		m[age]++;
	}
	for(auto it = m.begin();it!=m.end();it++){
    
    
		if(it->second>0){
    
    
			cout << it->first << ":" << it->second << endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45845039/article/details/108743625
今日推荐