标签:
#include<iostream> #include<cstring> using namespace std; bool check(int *numbers,int length,int number) { int times=0; for(int i=0;i<length;++i) { if(numbers[i]==number) times++; } bool ismorethanhalf=true; if(times*2<=length) { ismorethanhalf=false; } return ismorethanhalf; } int morethanhalf(int *numbers,int length) { int result=numbers[0]; int times=1; for(int i=1;i<length;++i) { /*if(times==0) { result=numbers[i]; times=1; }*/ if(numbers[i]==result) times++; else {times--; if(times==0) { result=numbers[i]; times=1; } } } if(!check(numbers,length,result)) result=0; return result; } int main() { //int numbers[6]={2,2,2,3,3,3}; //cout<<morethanhalf(numbers,6); char a[5]; int b=10; itoa(b,a,10); cout<<a<<endl; cout<<strlen(a)<<endl; system("pause"); return 0; }
标签:
原文地址:http://blog.csdn.net/qq_22335577/article/details/44828857