# 一个算法作业问题：细胞矩阵扫描问题[关闭]

##### 问题描述投票：0回答：2

EG

``````#include <stdio.h>

int main()
{
int n,array[100];
int i = 0;
int sum = 0;

if(scanf("%d",&n)){};
for(i=0;i<n;i++){
if(scanf("%d",&array[i])){}
}

for(i=0;i<n;i++)
{
if(array[i] >= array[i+1])
{
if(array[i+1] >= array[i+2])
{
if(array[i+2] > array[i+3])
{
sum += array[i+1];
i++;
}
sum += array[i];
}
else if(array[i+1] < array[i+2])
{
if(array[i] >= array[i+2])
{
sum += array[i];
}
else if(array[i] < array[i+2])
{
sum += array[i+1];
i++;
}
}
}
else if(array[i] < array[i+1])
{
if(array[i+1] < array[i+2])
{
if(array[i] < array[i+2])
{
sum += array[i+1];
i++;
}
else if(array[i] >= array[i+2])
{
sum += array[i];
}
}
else if(array[i+1] >= array[i+2])
{
sum += array[i];
}
}
}

printf("%d", sum);

return 0;
}
``````

c algorithm loops
##### 2个回答
1

``````for(i=0;i<n;i++)
{
if(array[i] >= array[i+1])
``````

`i == n-1`当您访问`array[n]`这超出阵列的还是未初始化

``````for(i=0;i<n;i++)
``````

``````for(i=0; i<(n-2); i++)
``````

``````if(array[i+2] > array[i+3])
``````

``````if(((i + 3) < n) && (array[i+2] > array[i+3]))
``````

``````if(scanf("%d",&n)){};
``````

``````if ((scanf("%d",&n) != 1) || (n > 100) || (n <= 0)) {
puts("invalid number of cells");
return 0;
}
``````

0

1 -2 -5 -40 -35 7