1. 程式人生 > >7-58 求整數序列中出現次數最多的數

7-58 求整數序列中出現次數最多的數

7-58 求整數序列中出現次數最多的數(15 分)

本題要求統計一個整型序列中出現次數最多的整數及其出現次數。

輸入格式:

輸入在一行中給出序列中整數個數N(0<N1000),以及N個整數。數字間以空格分隔。

輸出格式:

在一行中輸出出現次數最多的整數及其出現次數,數字間以空格分隔。題目保證這樣的數字是唯一的。

輸入樣例:

10 3 2 -1 5 3 4 3 0 3 2

輸出樣例:

3 4
#include <stdio.h>

int main()
{
	int n;
	scanf("%d",&n);
	int temp[n];
	for(int i = 0;i < n; i++)
	{
		scanf("%d",&temp[i]);
	}
	int a[n];//記錄次數的陣列 
	for(int i = 0; i < n; i++)
	{
		int count = 0;
		for(int j = 0; j < n ;j++)
		{
			if(temp[i] == temp[j]){
				count++;
				a[i]=count;
			}
		}
	}
	int ret;
	int max;
	for(int i = 0;i < n;i++){
		max = a[0];
		if(a[i] >= max){ 
			max = a[i];
			ret = i;
		} 
	}
	printf("%d %d",temp[ret],a[ret]);
	
	return 0;
}