[Hash散列] 7-1 统计工龄 (15分)

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

输入格式:

输入首先给出正整数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

思路:哈希散列存进一维数组,然后就输出吧!

 1 #include<iostream>
 2 #include<string>
 3 #define N 51
 4 using namespace std;
 5 int main()
 6 {
 7     int n,tmp,a[N]={0};
 8     cin>>n;
 9     for(int i=0;i<n;i++)
10     {
11         cin>>tmp;
12         a[tmp]++;
13     }
14     for(int k=0;k<=N;k++)
15     {
16         if(a[k]!=0)
17         printf("%d:%d\n",k,a[k]);
18     }
19     return 0;
20 }

原文地址:https://www.cnblogs.com/luoyoooo/p/12215700.html

时间: 2024-08-30 13:12:09

[Hash散列] 7-1 统计工龄 (15分)的相关文章

PTA 10-排序4 统计工龄 (20分)

题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/721 5-13 统计工龄   (20分) 给定公司NN名员工的工龄,要求按工龄增序输出每个工龄段有多少员工. 输入格式: 输入首先给出正整数NN(\le 10^5≤10?5??),即员工总人数:随后给出NN个整数,即每个员工的工龄,范围在[0, 50]. 输出格式: 按工龄的递增顺序输出每个工龄的员工个数,格式为:"工龄:人数".每项占一行.如果人数为0则不输出该项. 输入样

2020年2月24日09:06:11,Hash散列

问题描述 /** 有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,住址..),当输入该员工的id时,* 要求查找到该员工的所有信息.* ?要求: 1)不使用数据库,,速度越快越好=>哈希表(散列)* 2)添加时,保证按照id从低到高插入[课后思考:如果id不是从低到高插入,但要求各条链表仍是从低到高,怎么解决?]* 3)使用链表来实现哈希表, 该链表不带表头** */ 代码实现 package day0223 /* * 有一个公司,当有新的员工来报道时,要求将该员工的

&lt;数据结构与算法&gt;&lt;基础&gt;Hash散列

1.Hash的基本原理 总共有M-1个桶,hash(key)指向一个特定的桶. 2.Hash function散列函数 略 3.哈希冲突及解决 闭合定址(closed addressing): linked—list chaining:每个桶存放一个指针,冲突的词条组织成列表.新进来的插在第一个和第二个之间. 缺点是  1.指针需要额外空间:2.节点需要动态申请 开放定址(open addressing/closed hashing): 为每个桶事先约定若干备用桶,它们构成一个查找链(probi

PAT Advanced 1092 To Buy or Not to Buy (20) [Hash散列]

题目 Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the owner of the shop would only sell the strings in whole pieces. Hence Eva must c

Java解决Hash(散列)冲突的四种方法--开放地址法(线性探测,二次探测,伪随机探测)、链地址法、再哈希、建立公共溢出区

最近时间有点紧,暂时先放参考链接了,待有时间在总结一下: 查了好多,这几篇博客写的真心好,互有优缺点,大家一个一个看就会明白了: 参考 1. 先看这个明白拉链法(链地址法),这个带源码,很好看懂,只不过是只讲了拉链法一种: 2. 再看这个比较全的,四种全讲了,链接,这篇比较形象,有图.但是这两篇都没有仔细介绍优缺点: 3. 最后看优缺点,点击这里: 原文地址:https://www.cnblogs.com/gjmhome/p/11372883.html

PAT Advanced 1041 Be Unique (20) [Hash散列]

题目 Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1, 10^4]. The first one who bets on a unique number wins. For example, if there ar

PAT Advanced 1050 String Subtraction (20) [Hash散列]

题目 Given two strings S1 and S2, S = S1 – S2 is defined to be the remaining string afer taking all the characters in S2 from S1. Your task is simply to calculate S1 – S2 for any given strings. However, it might not be that simple to do it fast. Input

PAT Advanced 1050 Broken Keyboard (20) [Hash散列]

题目 On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen.Now given a string that you are supposed to type, and the string that you actually type out,

数据结构与算法题目集(中文)——5-13 统计工龄 (20分)——桶排序

给定公司NN名员工的工龄,要求按工龄增序输出每个工龄段有多少员工. 输入格式: 输入首先给出正整数NN(\le 10^5≤10?5??),即员工总人数:随后给出NN个整数,即每个员工的工龄,范围在[0, 50]. 输出格式: 按工龄的递增顺序输出每个工龄的员工个数,格式为:"工龄:人数".每项占一行.如果人数为0则不输出该项. 输入样例: 8 10 2 0 5 7 2 5 2 输出样例: 0:1 2:3 5:2 7:1 10:1  时间限制:400ms 内存限制:64MB 代码长度限制