#include <iostream>
#include <cstdlib>
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)
{
if (Length == 0) {
cout << "不存在" << endl;
return 0;
}
int flag = Length / 4;
if (flag == 0) {
cout << array[0] << endl;
return 0;
}
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 <iostream>
#include <cstdlib>
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++) {
<span style="white-space:pre"> </span> if (i + flag < Length && array[i] == array[i + flag]) {
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span> cout << "i = " << i << ", data = " << array[i] << endl;
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span> break;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>}
if (i >= Length) {
<span style="white-space:pre"> </span>cout << "不存在" << endl;
<span style="white-space:pre"> </span>}
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;
}