#include#include using namespace std;int single(int array[],int Length);int main(int argc , char *argv[]){ int a[]={1,1,1,2,2,3,4,4,4,5,6,7,8,8,8,8,8,8,8,9,9,9,9,9}; int aLen = sizeof a /sizeof a[0]; single(a, aLen); return 0;}int single(int array[],int Length){ int flag = Length / 4; cout << "Length = " << Length << " , Length / 4 = " << flag << endl; int f = false; int i =1 , s_len=0; int len =1; int min; for (i = 1; i < Length; i++) { if(array[i]==array[i-len]) { len++; } else { if (len > flag) { if (f == false) { min = array[i-1]; } f = true; } cout << "len = " << len << ", data = " << array[i-1] << endl; len = 1; } } if (Length > 0) { cout << "len = " << len << ", data = " << array[Length-1] << endl; } if (f) { cout << min << endl; } else { cout << "不存在" << endl; } return 0;}
// 简单点的写法#include#include using namespace std;int single(int array[],int Length){ int flag = Length / 4; cout << "Length = " << Length << " , Length / 4 = " << flag << endl; flag = flag ? flag - 1 : 0; int i; for(i = 0; i < Length; i++) { if (i + flag < Length && array[i] == array[i + flag]) { cout << "i = " << i << ", data = " << array[i] << endl; break; } } if (i >= Length) { cout << "不存在" << endl; } return 0;}int main(int argc , char *argv[]){ int a[]={1,1,1,2,2,3,4,4,4,5,6,7,8,8,8,8,8,8,8,9,9,9,9,9}; //int a[]={1,1,1,2,2,3,4,4,4,5,6,7,8,8,8,8,9,9,9,9}; int aLen = sizeof a /sizeof a[0]; single(a, aLen); return 0;}