题干:
编写一个程序,读入一组整数,这组整数是按照从小到大的顺序排列的,它们的个数N也是由用户输入的,最多不会超过20。然后程序将对这个数组进行统计,把出现次数最多的那个数组元素值打印出来。如果有两个元素值出现的次数相同,即并列第一,那么只打印较小的那个值。例如,假设用户输入的是“100 150 150 200 250”,则输出为150。
输入:
6
100 150 150 200 200 250
输出:
150
解题报告:
乱搞。倒着遍历可以减少代码量。
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e5 + 5;
ll a[MAX];
int main()
{
ll n;
cin>>n;
for(int i = 1; i<=n; i++) cin>>a[i];
ll ans = a[n],cnt=1,tmpcnt = 1;
for(int i = n-1; i>=1; i--) {
if(a[i] == a[i+1]) {
tmpcnt++;
}
else tmpcnt = 1;
if(tmpcnt >= cnt) ans = a[i],cnt = tmpcnt;
}
cout << ans <<endl;
return 0 ;
}