99999999海岛帝国后传:算法大会
#include <iostream> using namespace std; int MajorityElement( int *a, int n) { int k, i; int elem; k = 1; elem = a[0]; for( i = 1; i <n; i++ ) { if( a[i]==elem ) k++; else { k--; if( k == 0 ) { k = 1; elem = a[i]; } } } return elem; } int main(int argc, char* argv[]) { int n; cin>>n; int a[n]; for(int i=1;i<n+1;i++) cin>>a[i]; cout<<MajorityElement(a,sizeof(a)/sizeof(a[0]))<<endl; }
PS:ljx的代码