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

时间:2018-05-16 06:06:04

标签: c matrix diagonal

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

#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

2 个答案:

答案 0 :(得分:2)

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

我建议你只计算进行打印的循环中的最小值。

类似的东西:

#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;
}

答案 1 :(得分: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