10000的随机数组由ox组成,查找数组中oox...x(任意x)oox....x(任意x)o的个数

enum {
DATA_SIZE = ,
}; enum enum_status {
STATUS_FOUND = ,
STATUS_ERR,
STATUS_NOT_FOUND, //ALL 'X'
}; enum_status findOOX(int data[DATA_SIZE], int start, int& index) {
for(int i = start; i < DATA_SIZE - ; i++) {
if(data[i] == 'O' && data[i + ] == 'O' && data[i + ] == 'X') {
index = i;
return STATUS_FOUND;
} if(data[i] == 'O' && (data[i + ] != 'O' || data[i + ] != 'X')) {
index = i;
return STATUS_ERR;
}
}
return STATUS_NOT_FOUND;
} bool findO(int data[DATA_SIZE], int start, int& index) {
for(int i = start; i < DATA_SIZE; i++) {
if(data[i] == 'O') {
index = i;
return true;
}
}
return false;
} int test_main(int data[DATA_SIZE]) {
enum_status stat = STATUS_NOT_FOUND;
int count = ;
int index = ; for(int i = ; i < DATA_SIZE; i++) {
stat = findOOX(data, i, index);
if(stat == STATUS_NOT_FOUND) {
break;
} if(stat == STATUS_ERR) {
i = index; //TODO
continue;
}
i = index + ; stat = findOOX(data, i, index);
if(stat == STATUS_NOT_FOUND) {
break;
} if(stat == STATUS_ERR) {
i = index; //TODO
continue;
}
i = index + ; if(findO(data, i, index)) {
count++;
i = index;
}
else {
break;
} } return count;
}

用于生成测试用例的demo:

#include <stdio.h>
#include <stdlib.h> #define SIZE 10000 int test_main(int data[SIZE]); static int data[SIZE]; static void build_data(void)
{
for (int a = ; a < SIZE; a++)
{
data[a] = (rand() % ) ? 'O' : 'X';
}
} void main(void)
{
for (int l = ; l < ; l++)
{
build_data(); printf("%d\n", test_main(data));
}
system("pause");
}
05-11 08:07