如何高效率的统计一个序列中位置连续某给定值的个数
题目 有一序列A{a1,a2,a3,...,an}, 给出一个数b,若序列A中有与b相等的元素,
要求给出与b相等的位置连续元素的个数,并且要求记录位置连续的元素在A中的位置,
比如 序列A{1,3,1,1,1,2,1,1},给定1,那么与1相等且位置连续的个数是3,且位置是3。
[解决办法]
int main(){ int array[] = {1,2,32,4,4,5,5,67,5,6,8,9,1,1,5,5,5,7,8,9,9,5,5,4,4,4,4,4,4,8,4,4,9,9}; int length = sizeof(array)/sizeof(int); int index = 9;//指定的数字 int max =0, i = 0, j = 0, k = 0, count = 0; while(i < length) { while(array[i] != index&&i < length) i++; j = i; while(array[i] == index&&i < length) i++; count = i - j; if(max <= count) { max = count; k = j; } } printf("指定数字:[%d],下标:[%d],最大连续个数:[%d]\n",index,k,max); return 0;}