7-11 求整數序列中出現次數最多的數 (15分)
本題要求統計一個整型序列中出現次數最多的整數及其出現次數。
輸入格式:
輸入在一行中給出序列中整數個數N(0<N≤1000),以及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 a[n];
int i;
for(i=0;i<n;i++)
scanf("%d",a+i);
int j;
int max;
int temp;
int count=0;
for(i=0;i<n;i++)
{
temp=1;
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
temp++;
}
if(temp>count)
{
max=a[i];
count=temp;
}
}
printf("%d %d\n",max,count);
return 0;
}