一个算法问题,大家帮忙解决
读一个整型数组,长度暂设为8
数组中的0可以代表任意数
数组中0可以重复,而其他数则不能重复.
当有前后连续的5个数据时,返回TURE
否则返回FLASE
例如:0 1 2 0 4 78 9 63 则返回TURE
12 13 0 0 16 86 96 36 返回TURE
15 0 17 0 0 5 87 3 返回TURE
1 0 6 8 9 11 58 66 返回FLASE
[解决办法]
楼主,我这个不漂亮的代码可以实现你要的功能
bool isSerial(int *nums,int nBit){ int temp = *nums; int iPos = 0; if (temp == 0) { int a = 0; int subTemp = 0; while (a < nBit) { if (*nums != 0) { subTemp = *nums; break; } nums++; a++; } while (a >= 0) { *nums-- = subTemp--; a--; } nums++; } temp = *nums++; int step = 1; while (iPos <= nBit) { if (*nums == 0) { step++; if (step == 5) { return true; } temp++; nums++; } else if((*nums - temp) == 1) { step++; if (step == 5) { return true; } temp = *nums++; } else { step = 1; temp = *nums++; } iPos++; } return false;}