09: Histogram

Total time limit: 1000ms memory limit: 65536kB
description
given a non-negative integer array, the number of occurrences of each number inside statistics. We only count the maximum number of array.

Assume Fmax (Fmax <10000) array is the largest number, then we only count the number of {0,1,2 ... Fmax} where each number appears.

Input
The first line n is the size of the array. 1 <= n <= 10000.
Followed by a row of n elements of the array.
Output
sequentially outputs the number of occurrences of each number, a line number. If there have been no, the output is 0.
For example in an array, the maximum number is three, so we only count the {0,1,2,3} occurrence frequency.
Sample input
. 5
. 1. 1 2. 3. 1
Sample Output
0
. 3
. 1
. 1

#include<iostream>
#include<cstring>
using namespace std;
int main(){
	int n;
	cin>>n;
	
	int max=-1;
	int a[10005];
	//数组用来记录每个数出现的次数(最大的数<10000),初始时设置为0
	memset(a,0,sizeof(a));
	
	int temp;
	//temp临时变量,用来记录输入 
	for(int i=0;i<n;i++){
		cin>>temp;
	//输入一个数,这个数的次数自增 
		a[temp]++;
	//找到并记下最大的数 
		if(temp>max)
		max=temp;
	}
	
	//依次输出从0到max中每个数出现的次数 
	for(int i=0;i<=max;i++){
		cout<<a[i]<<endl;
	}
	
	
	return 0;
} 
Published 36 original articles · won praise 0 · Views 341

Guess you like

Origin blog.csdn.net/weixin_44437496/article/details/104044341
09