HDOJ-1029(简单dp或者排序)
Ignatius and the Princess IV
hdoj-1029
- 这里主要是先排序,因为要找出现了一半以上的数字,所以出现的数字一定在中间
方法一:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n;
int a[10000007];
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
cout<<a[n/2]<<endl;
}
//system("pause");
return 0;
}
方法二:简单计算出每个数字出现的次数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n;
int a[10000007];
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
//cout<<a[n/2]<<endl;
int k=0;
for(int i=0;i<n-1;i++){
if(a[i]!=a[i+1]){
//cout<<k<<endl;
if(k+1>=(n+1)/2){
cout<<a[i]<<endl;
break;
}
k=0;
}else{
k++;
if(k+1>=(n+1)/2){
cout<<a[i]<<endl;
break;
}
}
}
}
//system("pause");
return 0;
}
Either Excellent or Rusty