2

Please, help me to find and print all square submatrices of square matrix from big to small square matrices in C programming language

I wrote code that works wrong:

int main() {
    int mtrx_size = 8;
    int mat[8][8] = {
        { 1, 2, 3, 4, 5, 6, 7, 8},
        { 9,10,11,12,13,14,15,16},
        {17,18,19,20,21,22,23,24},
        {25,26,27,28,29,30,31,32},
        {33,34,35,36,37,38,39,40},
        {41,42,43,44,45,46,47,48},
        {49,50,51,52,53,54,55,56},
        {57,58,59,60,61,62,63,64}
    };

    int i,j;
    int sub_mtrx_size;

    for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 1 ; sub_mtrx_size--)
    {
        for(i = 0; i < sub_mtrx_size; i++)
        {
            for(j = 0; j < sub_mtrx_size; j++)
            {
                printf("%3d ", mat[i][j]);
            }
            printf("\n");
        }
        printf("\n");

    }
    return 0;

Here I need to find all 8x8, 7x7, 6x6, 5x5, 4x4, 3x3 and 2x2 submatrices.

4

3 回答 3

2

您的代码只是为每个尺寸打印一个子矩阵,位于矩阵的左上角。您需要添加 i 和 j 偏移来获取所有位置的子矩阵:

#include <stdio.h>

int main() {
    int mtrx_size = 8;
    int mat[8][8] = {
        { 1, 2, 3, 4, 5, 6, 7, 8},
        { 9,10,11,12,13,14,15,16},
        {17,18,19,20,21,22,23,24},
        {25,26,27,28,29,30,31,32},
        {33,34,35,36,37,38,39,40},
        {41,42,43,44,45,46,47,48},
        {49,50,51,52,53,54,55,56},
        {57,58,59,60,61,62,63,64}
    };

    int i, j, ioff, joff, off_cnt;
    int sub_mtrx_size;

    for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 1 ; sub_mtrx_size--) {
        off_cnt = mtrx_size - sub_mtrx_size + 1;
        for (ioff = 0; ioff < off_cnt; ioff++) {
            for (joff = 0; joff < off_cnt; joff++) {
                for (i = 0; i < sub_mtrx_size; i++) {
                    for (j = 0; j < sub_mtrx_size; j++) {
                        printf("%3d ", mat[i+ioff][j+joff]);
                    }
                    printf("\n");
                }
                printf("\n");
            }
        }
    }

    return 0;
}
于 2015-11-08T14:46:25.883 回答
0

通用 nxm 矩阵的 Java 实现:

private static void printSubMatrix(int[][] mat) {
    int rows=mat.length;
    int cols=mat[0].length;

    //prints all submatrix greater than or equal to 2x2
    for (int subRow = rows; subRow >= 2; subRow--) {
        int rowLimit = rows - subRow + 1;
        for (int subCol = cols; subCol >= 2; subCol--) {
            int colLimit = cols - subCol + 1;
            for (int startRow = 0; startRow < rowLimit; startRow++) {
                for (int startCol = 0; startCol < colLimit; startCol++) {

                    for (int i = 0; i < subRow; i++) {
                        for (int j = 0; j < subCol; j++) {
                            System.out.print(mat[i + startRow][j + startCol] + " ");
                        }
                        System.out.print("\n");
                    }
                    System.out.print("\n");

                }
            }
        }
    }

}
于 2016-10-05T19:49:24.483 回答
0
#include <stdio.h>

int main() {
   int mtrx_size = 8;
 int mat[8][8] = {
    { 1, 2, 3, 4, 5, 6, 7, 8},
    { 9,10,11,12,13,14,15,16},
    {17,18,19,20,21,22,23,24},
    {25,26,27,28,29,30,31,32},
    {33,34,35,36,37,38,39,40},
    {41,42,43,44,45,46,47,48},
    {49,50,51,52,53,54,55,56},
    {57,58,59,60,61,62,63,64}
};

int i, j, ioff, joff, off_cnt;
int sub_mtrx_size;
/* if we make terminating condition sub_mtrx_size>=1 then we will have all 
possible square sub matrices */
for(sub_mtrx_size = mtrx_size; sub_mtrx_size >= 1 ; sub_mtrx_size--) {
    off_cnt = mtrx_size - sub_mtrx_size + 1;
    for (ioff = 0; ioff < off_cnt; ioff++) {
        for (joff = 0; joff < off_cnt; joff++) {
            for (i = 0; i < sub_mtrx_size; i++) {
                for (j = 0; j < sub_mtrx_size; j++) {
                    printf("%3d ", mat[i+ioff][j+joff]);
                }
                printf("\n");
            }
            printf("\n");
        }
    }
}

return 0;
}
于 2020-06-08T19:23:51.010 回答