版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xj13821328013/article/details/75103685
刚开始看到这个题目觉得简单得不行,考虑是将数字存入数组,然后再进行遍历,可以考虑一下这样的复杂度是n^2,我觉得思路很清晰就没敲代码,看自己去年提交过就看了下自己当时的代码,附当时代码
#include<iostream>
#include<stdio.h>
#include<string.h>
#define M 50000
using namespace std;
int tem,b[M];
int main(){
int n,i,j;
while(scanf("%d",&n)!=EOF){
memset(b,0,sizeof(b));
for(i=0;i<n;i++){
scanf("%d",&tem);
b[tem]++;
if(b[tem]>=(n+1)/2)
j=tem;
}
printf("%d\n",j);
}
return 0;
}
简单好多,现在觉得n^2的思路肯定会超时,将数组的下标和存的数交换一下位置,代码一下简单了好多= =佩服当时的自己。