方阵对角线中的最小元素?

Minimum element in diagonal strips of square matrix?

我需要在方阵的每个对角线条(垂直于主对角线)中找到最小值。我正在使用的代码实际上确实输出了每个条带,但由于某种原因它没有找到最小元素。

#include<stdio.h>

int main(){
    int min[10], x[3][3] = { 1, 2, 3,
                             4, 5, 6,
                             7, 8, 9};
    int n = 3, d, j, z, i, k;

    for (d = 0, i = 0; d < 2 * n - 1; d++, i++){
        printf("D %d: ", d);
        z = (d < n) ? 0 : d - n + 1;
        for (j = z; j <= d - z; j++){
            printf("%d ", x[j][d - j]);
            if(d == 0 || d == 2 * n - 2){
                min[i] = x[j][d - j];
                break;
            }
            min[i] = x[j][d - j];
            for (k = j + 1; k <= d - z; k++){
                if (min[i] > x[k][d - k])
                    min[i] = x[k][d - k];
            }
        }
        printf("\n");
    }
    printf("\n");
    for (i = 0; i < 2 * n - 1; i++)
        printf("min = %d\n", min[i]);

    return 0;
}

输出:

D 0: 1
D 1: 2 4
D 2: 3 5 7
D 3: 6 8
D 4: 9
min = 1
min = 4
min = 7
min = 8
min = 9

但在这种情况下应该是

min = 1
min = 2
min = 3
min = 6
min = 9

我认为你把它弄得比需要的更复杂,我发现很难理解你的算法是什么。

我建议您在进行打印的循环中简单地计算最小值。

类似于:

#include<stdio.h>

int main(){
    int min[10], x[3][3] = { {1, 2, 3},
                             {4, 5, 6},
                             {7, 8, 9}};
    int n = 3, d, j, z, i;

    for (d = 0, i = 0; d < 2 * n - 1; d++, i++){
        printf("D %d: ", d);
        z = (d < n) ? 0 : d - n + 1;
        for (j = z; j <= d - z; j++){
            printf("%d ", x[j][d - j]);
            if (j == z)
            {
              // First time for this strip
              // So just initialize min to current element
              min[d] = x[j][d - j];
            }
            else if (min[d] > x[j][d - j])
            {
              // Current element is less than min
              // So overwrite min with current element
              min[d] = x[j][d - j];
            }
            if(d == 0 || d == 2 * n - 2){
                break;
            }
        }
        printf("\n");
    }
    printf("\n");
    for (i = 0; i < 2 * n - 1; i++)
        printf("min = %d\n", min[i]);

    return 0;
}

我认为您可以用更少的特殊条件来控制循环。原始循环中的主要问题是您有三重嵌套循环,其中双重嵌套循环就足够了。这意味着您经常将 min 设置为新值。

此代码打印出数组元素的下标以及值。

#include <stdio.h>

int main(void)
{
    int min[10], x[3][3] =
    {
        { 1, 2, 3, },
        { 4, 5, 6, },
        { 7, 8, 9, },
    };
    int n = 3;

    for (int d = 0; d < 2 * n - 1; d++)
    {
        printf("D %d: ", d);
        int z = (d < n) ? 0 : d - n + 1;
        printf("A[%d][%d] = %d: ", z, d - z, x[z][d - z]);
        min[d] = x[z][d - z];
        for (int j = z + 1; j <= d - z; j++)
        {
            printf("A[%d][%d] = %d: ", j, d - j, x[j][d - j]);
            if (x[j][d-j] < min[d])
                min[d] = x[j][d-j];
        }
        printf("min[%d] = %d\n", d, min[d]);
    }

    printf("\n");

    for (int i = 0; i < 2 * n - 1; i++)
        printf("min = %d\n", min[i]);

    return 0;
}

输出:

D 0: A[0][0] = 1: min[0] = 1
D 1: A[0][1] = 2: A[1][0] = 4: min[1] = 2
D 2: A[0][2] = 3: A[1][1] = 5: A[2][0] = 7: min[2] = 3
D 3: A[1][2] = 6: A[2][1] = 8: min[3] = 6
D 4: A[2][2] = 9: min[4] = 9

min = 1
min = 2
min = 3
min = 6
min = 9