2D阵列有一个问题是
给定6 * 6矩阵,我们必须打印矩阵中找到的最大(最大)沙漏总和。 沙漏被描述为:
a b c
d
e f g
示例输入
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0
示例输出
19
解释
样本矩阵包含以下沙漏:
1 1 1 1 1 0 1 0 0 0 0 0
1 0 0 0
1 1 1 1 1 0 1 0 0 0 0 0
0 1 0 1 0 0 0 0 0 0 0 0
1 1 0 0
0 0 2 0 2 4 2 4 4 4 4 0
1 1 1 1 1 0 1 0 0 0 0 0
0 2 4 4
0 0 0 0 0 2 0 2 0 2 0 0
0 0 2 0 2 4 2 4 4 4 4 0
0 0 2 0
0 0 1 0 1 2 1 2 4 2 4 0
最大总和(19)的沙漏是
2 4 4
2
1 2 4
我编写了一个程序,我已经编写了一个计算沙漏总和的函数。现在我已经创建了一个循环,可以为每行四个沙漏调用此函数。并且每四行可以制作一个沙漏。
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int sum(int a[6][6],int i,int j)
{
int n=i+3;
int m=j+3;
int sum=0;
for(i;i<n;i++)
{
for(j;j<m;j++)
{
if(i==n-2)
{
sum += a[i][j+1];
break;
}
else
sum += a[i][j];
}
}
// printf("%d\t",sum);
return sum;
}
int main(){
int arr[6][6];
int i,j,n,k;
int max=0;
for(int arr_i = 0; arr_i < 6; arr_i++){
for(int arr_j = 0; arr_j < 6; arr_j++){
scanf("%d",&arr[arr_i][arr_j]);
}
}
for(int i=0;i<4;i++)
{
k=0;
while(k<4)
{
n=sum(arr,i,k);
// printf("%d\t",n);
k++;
if(n>max)
max=n;
}
}
printf("%d",max);
return 0;
}
任何人都可以告诉我哪里出错了,或者这种方法不能解决这个问题吗?
我的程序打印10
作为输出。
答案 0 :(得分:2)
错误可能在您的sum
函数中。你做这件事的方式有点矫枉过正,对你这样做会更简单(也更可读!):
#define GRID_SIZE (6)
int sum(int a[GRID_SIZE][GRID_SIZE], int i, int j)
{ // Define an hourglass by the index i,j of its central element
int sum = a[j-1][i-1] + a[j-1][i] + a[j-1][i+1] +
a[j][i] +
a[j+1][i-1] + a[j+1][i] + a[j+1][i+1];
return sum;
}
然后确保使用合理的值(在[1,len-2]中)进行迭代:
for (int i = 1; i < (GRID_SIZE-1); i++)
{
for (int j = 1; j < (GRID_SIZE-1); j++)
{
n = sum(arr, i, j);
if (n > max)
max = n;
}
}
修改:检查它是否有效:http://www.cpp.sh/46jhy
谢谢,这很有趣: - )。
PS:确保你检查一些编码标准文件,从长远来看,它会让你的生活变得更轻松,只需搜索“C代码格式标准”,并习惯尝试使用你喜欢的。除非你自己做一些新的事情,否则你可能必须遵循一个标准,甚至可能不知道哪一个,所以熟悉一般规则并习惯跟随一个,无论你喜欢哪一个。答案 1 :(得分:1)
这里有一些矫枉过正 - 四种不同的方式来编写沙漏总和&#39;功能。出于user3629249 comment中列出的原因,我已将函数重命名为hourglass_N
(对于1中的N)。在这些变体中,hourglass_1()
非常适合这种特殊形状,但hourglass_2()
更容易适应其他形状。
测试代码正确处理具有负最大总和的矩阵。
#include <assert.h>
#include <stdio.h>
enum { ARR_SIZE = 6 };
static int hourglass_1(int a[ARR_SIZE][ARR_SIZE], int i, int j)
{
assert(i >= 0 && i < ARR_SIZE - 2 && j >= 0 && j < ARR_SIZE - 2);
int sum = a[i+0][j+0] + a[i+0][j+1] + a[i+0][j+2]
+ a[i+1][j+1] +
a[i+2][j+0] + a[i+2][j+1] + a[i+2][j+2];
return sum;
}
static int hourglass_2(int a[ARR_SIZE][ARR_SIZE], int i, int j)
{
assert(i >= 0 && i < ARR_SIZE - 2 && j >= 0 && j < ARR_SIZE - 2);
static const int rows[] = { 0, 0, 0, 1, 2, 2, 2 };
static const int cols[] = { 0, 1, 2, 1, 0, 1, 2 };
enum { HG_SIZE = sizeof(rows) / sizeof(rows[0]) };
int sum = 0;
for (int k = 0; k < HG_SIZE; k++)
sum += a[rows[k]+i][cols[k]+j];
return sum;
}
static int hourglass_3(int a[ARR_SIZE][ARR_SIZE], int i, int j)
{
assert(i >= 0 && i < ARR_SIZE - 2 && j >= 0 && j < ARR_SIZE - 2);
int sum = 0;
for (int i1 = 0; i1 < 3; i1++)
{
for (int j1 = 0; j1 < 3; j1++)
{
if (i1 == 1)
{
sum += a[i + i1][j + j1 + 1];
break;
}
else
sum += a[i + i1][j + j1];
}
}
return sum;
}
static int hourglass_4(int a[ARR_SIZE][ARR_SIZE], int i, int j)
{
assert(i >= 0 && i < ARR_SIZE - 2 && j >= 0 && j < ARR_SIZE - 2);
int n = i + 3;
int m = j + 3;
int sum = 0;
for (int i1 = i; i1 < n; i1++)
{
for (int j1 = j; j1 < m; j1++)
{
if (i1 == n - 2)
{
sum += a[i1][j1 + 1];
break;
}
else
sum += a[i1][j1];
}
}
return sum;
}
typedef int (*HourGlass)(int arr[ARR_SIZE][ARR_SIZE], int i, int j);
static void test_function(int arr[ARR_SIZE][ARR_SIZE], const char *tag, HourGlass function)
{
int max_sum = 0;
int max_row = 0;
int max_col = 0;
for (int i = 0; i < (ARR_SIZE-2); i++)
{
for (int j = 0; j < (ARR_SIZE-2); j++)
{
int n = (*function)(arr, i, j);
if (n > max_sum || (i == 0 && j == 0))
{
max_sum = n;
max_row = i;
max_col = j;
}
}
}
printf("%s: %3d at (r=%d,c=%d)\n", tag, max_sum, max_row, max_col);
}
int main(void)
{
int arr[ARR_SIZE][ARR_SIZE];
for (int i = 0; i < ARR_SIZE; i++)
{
for (int j = 0; j < ARR_SIZE; j++)
{
if (scanf("%d", &arr[i][j]) != 1)
{
fprintf(stderr, "Failed to read integer (for row %d, col %d)\n", i, j);
return 1;
}
}
}
test_function(arr, "hourglass_1", hourglass_1);
test_function(arr, "hourglass_2", hourglass_2);
test_function(arr, "hourglass_3", hourglass_3);
test_function(arr, "hourglass_4", hourglass_4);
return 0;
}
对于各种不同的数据集,代码会产生正确的答案。
设置1:
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0
hourglass_1: 19 at (r=3,c=2)
hourglass_2: 19 at (r=3,c=2)
hourglass_3: 19 at (r=3,c=2)
hourglass_4: 19 at (r=3,c=2)
设置2:
-1 -1 -1 +0 +0 +0
+0 -1 +0 +0 +0 +0
+1 -1 +1 +0 +0 +0
+0 +0 -2 +4 -4 +0
+0 +0 +0 +2 +0 +0
+0 +0 +1 +2 -4 +0
hourglass_1: 7 at (r=2,c=2)
hourglass_2: 7 at (r=2,c=2)
hourglass_3: 7 at (r=2,c=2)
hourglass_4: 7 at (r=2,c=2)
设置3:
-7 -2 -9 -7 -4 -4
-6 0 -7 -5 -8 -1
-9 -1 -2 -1 -3 -3
-9 -1 -3 -6 -2 -9
-8 -1 -3 -7 -7 -9
0 -3 -5 -2 -2 -5
hourglass_1: -18 at (r=2,c=1)
hourglass_2: -18 at (r=2,c=1)
hourglass_3: -18 at (r=2,c=1)
hourglass_4: -18 at (r=2,c=1)
设置4:
-7 -7 0 -7 -8 -7
-9 -1 -5 -6 -7 -8
-2 0 -7 -7 -6 -3
-5 -3 -1 -6 -3 -1
-3 -5 0 -5 0 -7
-1 -2 -8 -8 -9 -9
hourglass_1: -20 at (r=2,c=0)
hourglass_2: -20 at (r=2,c=0)
hourglass_3: -20 at (r=2,c=0)
hourglass_4: -20 at (r=2,c=0)
答案 2 :(得分:0)
#include <stdio.h>
int main() {
int numbers[6][6];
for (int i = 0; i < 6; i++){
for (int j = 0; j < 6; j++){
scanf("%d",&numbers[i][j]);
}
}
int currentHourGlass;
int largestSum = -999;
for (int i = 1; i < 5; i++){
for (int j = 1; j < 5; j++){
currentHourGlass = 0;
currentHourGlass += numbers[i-1][j-1];
currentHourGlass += numbers[i-1][j];
currentHourGlass += numbers[i-1][j+1];
currentHourGlass += numbers[i][j];
currentHourGlass += numbers[i+1][j-1];
currentHourGlass += numbers[i+1][j];
currentHourGlass += numbers[i+1][j+1];
if (currentHourGlass > largestSum)
{
largestSum = currentHourGlass;
}
}
}
printf("%d", largestSum);
}