如果您仍在努力寻找 ,中的间隙Array
(间隙定义为至少needed
长度的连续零元素的数量),那么找到所有间隙的简单直接的方法是移动“滑动窗口”needed
数组的长度,检查窗口内的所有值是否为零。如果它们都为零,则您发现了一个间隙,如果没有,则移动到下一个索引Array
并重复。
这个概念相当简单,但图片可能会有所帮助(或尝试图片)
+-------+
| | <= window to slide over array
| |
+---+---+---+---+---+---+---+---+...
| 0 | 0 | 1 | 0 | 1 | 0 | 1 | 1 | <= array values
+---+---+---+---+---+---+---+---+...
0 1 2 3 4 5 6 7 8 <= array indexes
| |
+-------+
如上所示,您有显示的前九个元素Array
,以及下面每个元素的相应索引。因为你needed
是2
你有一个窗口,2-elements
你将从头到尾Array
检查其中的值。这可以通过两个嵌套循环简单地完成,外循环循环 while i = needed; i < num_elements; i++
,然后内循环从 迭代j = i - needed; j < i; j++
。
要捕获内部间隙的Array
位置,请使用第二个数组(我称为gaps
),其中包含与初始化的相同数量的元素Array
All zero。当您找到一个区域内Array
有needed
许多连续元素时,您只需递增gaps[j]++;
以将值设置为gaps[j]
from 0
to 1
。(取决于 的范围,如果超出范围j
,您可能需要增加。gaps[i-needed]++;
j
当您完成从头到尾移动滑动窗口时,间隙开始所在的每个索引处的gaps
值将是。1
Array
一个实现滑动窗口的简单函数可以这样写:
/** find_gaps locates each sequence of all zero within 'array' of
* at least 'needed' length, the corresponding index within 'gaps'
* is incremented to identify the start of each gap, returns the
* number of gaps of 'needed' length in 'array'.
*/
int find_gaps (int *gaps, int *arr, int nelem, int needed)
{
int ngaps = 0; /* count of gaps found */
/* add code to validate parameters here */
memset (gaps, 0, nelem * sizeof *gaps); /* zero gaps array */
for (int i = needed; i < nelem; i++) { /* loop needed to end */
for (int j = i - needed; j < i; j++) { /* loop previous needed */
if (arr[j] != 0) /* if non-zero value, get next in array */
goto next; /* lowly 'goto' works fine here */
}
gaps[i-needed]++; /* increment index in gaps */
ngaps++; /* increment no. of gaps found */
next:;
}
return ngaps; /* return no. of gaps found */
}
(注意:内部循环可以替换memcmp
为检查值是否将所有字节设置为零以定位间隙)
使用图表作为指导,完成上述操作,find_gaps
并确保您准确了解发生了什么。如果没有,请告诉我。
把它放在一个简短的例子中,needed
作为程序的第一个参数(2
如果没有给出参数,则默认使用),您可以执行以下操作:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
/** find_gaps locates each sequence of all zero within 'array' of
* at least 'needed' length, the corresponding index within 'gaps'
* is incremented to identify the start of each gap, returns the
* number of gaps of 'needed' length in 'array'.
*/
int find_gaps (int *gaps, int *arr, int nelem, int needed)
{
int ngaps = 0; /* count of gaps found */
/* add code to validate parameters here */
memset (gaps, 0, nelem * sizeof *gaps); /* zero gaps array */
for (int i = needed; i < nelem; i++) { /* loop needed to end */
for (int j = i - needed; j < i; j++) { /* loop previous needed */
if (arr[j] != 0) /* if non-zero value, get next in array */
goto next; /* lowly 'goto' works fine here */
}
gaps[i-needed]++; /* increment index in gaps */
ngaps++; /* increment no. of gaps found */
next:;
}
return ngaps; /* return no. of gaps found */
}
int main (int argc, char **argv) {
int array[] = { 0,0,1,0,1,0,1,1,1,0,0,0,0,0,1,1,0,1,0,0,
0,1,1,0,1,0,0,0,1,0,1,1,0,0,1,0,1,0,0,1 },
nelem = sizeof array / sizeof *array, /* number of elements */
gaps[nelem], /* a VLA is fine here C99+, otherwise allocate */
ngaps = 0, /* no. of gaps found */
needed = argc > 1 ? strtol (argv[1], NULL, 0) : 2; /* (default: 2) */
if (errno) { /* validate strtol conversion succeeded */
perror ("strtol-argv[1]");
return 1;
}
/* find the number of gaps, storing beginning index in 'gaps' array */
ngaps = find_gaps (gaps, array, nelem, needed);
printf ("gaps found: %d\n", ngaps); /* output number of gaps */
for (int i = 0; ngaps && i < nelem; i++) /* output index of gaps */
if (gaps[i])
printf (" gap at array[%2d]\n", i);
return 0;
}
(注意:您应该添加needed
小于 的检查nelem
,但这和任何其他验证都留给您作为练习)
示例使用/输出
$ ./bin/findgaps
gaps found: 11
gap at array[ 0]
gap at array[ 9]
gap at array[10]
gap at array[11]
gap at array[12]
gap at array[18]
gap at array[19]
gap at array[25]
gap at array[26]
gap at array[32]
gap at array[37]
检查至少 3 个零的间隙:
$ ./bin/findgaps 3
gaps found: 5
gap at array[ 9]
gap at array[10]
gap at array[11]
gap at array[18]
gap at array[25]
有几种方法可以解决这个问题,但滑动窗口可能是最直接的方法之一,而且滑动窗口在 C 语言中有许多其他应用程序,因此非常值得将其添加到您的工具箱中。如果您还有其他问题,请告诉我。