数据结构与算法题目集7-13——统计工龄

版权声明:我的GitHub:https://github.com/617076674。真诚求星! https://blog.csdn.net/qq_41231926/article/details/84679768

我的数据结构与算法题目集代码仓:https://github.com/617076674/Data-structure-and-algorithm-topic-set

原题链接:https://pintia.cn/problem-sets/15/problems/721

题目描述:

知识点:计数

思路:用一个大小为51的数组统计每个年龄的人数

时间复杂度是O(N)。空间复杂度是O(51)。

C++代码:

#include<iostream>

using namespace std;

int N, count[51];

int main(){
	fill(count, count + 51, 0);
	scanf("%d", &N);
	int num;
	for(int i = 0; i < N; i++){
		scanf("%d", &num);
		count[num]++;
	}
	for(int i = 0; i < 51; i++){
		if(count[i] != 0){
			printf("%d:%d\n", i, count[i]);
		}
	}
	return 0;
}

C++解题报告:

猜你喜欢

转载自blog.csdn.net/qq_41231926/article/details/84679768
今日推荐