问题描述
给定n个正整数,找出它们中出现次数最多的数。如果这样的数有多个,请输出其中最小的一个。
输入格式
输入的第一行只有一个正整数n(1 ≤ n ≤ 1000),表示数字的个数。
输入的第二行有n个整数s1, s2, …, sn (1 ≤ si ≤ 10000, 1 ≤ i ≤ n)。相邻的数用空格分隔。
输出格式
输出这n个次数中出现次数最多的数。如果这样的数有多个,输出其中最小的一个。
样例输入
6
10 1 10 20 30 20
样例输出
10
//#include<stdio.h>
//#include<stdlib.h>
//#include<algorithm>
//#include<cmath>
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <Stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
int a,b,c,d;
scanf("%d",&a);
int arr[a+1];
for(int i=0;i<a;i++)
{
scanf("%d",&arr[i]);
}
sort(arr,arr+a);
for(int i=0;i<a;i++)
cout<<arr[i]<<endl;
int temp=arr[0],ans=0,max=-1,count=1;
for(int i=0;i<a;i++)
{
count=0;
int j=i;
for(;arr[j]==temp;count++,j++)
cout<<"count"<<count<<" "<<j<<" "<<arr[j]<<" "<<temp<<endl;
if(count>max||(count==max&&arr[i]<ans))
{
cout<<ans<<" "<<count<<endl;
ans=arr[i];
max=count;
cout<<ans<<" "<<count<<endl;
}
temp=arr[i];
cout<<"temp"<<" "<<temp<<endl;
}
cout<<ans<<endl;
return 0;
}
以后回来看
别人的
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <Stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout << "[" << x << "]"
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w+", stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<LL> vec;
typedef vector<vec> mat;
const int MX = 1e3 + 5;
int A[MX];
int main() {
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &A[i]);
}
sort(A + 1, A + 1 + n);
int Max = 0, ans, l, r;
for(l = 1; l <= n; l = r + 1) {
for(r = l; r < n && A[l] == A[r + 1]; r++);
int cnt = r - l + 1;
if(cnt > Max || (cnt == Max && A[l] < ans)) {
Max = cnt; ans = A[l];
}
}
printf("%d\n", ans);
return 0;
}