Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

C语言算法题:矩阵转置

这个是我的代码:

#include <stdio.h>

int input_M = 0, input_N = 0;

int main() {
    scanf("%d%d", &input_M, &input_N);
    int matrix1[input_M][input_N];
    int matrix2[input_N][input_M];
    for (int i = 0; i < input_M; ++i) {
        for (int j = 0; j < input_N; ++j) {
            scanf("%d", &matrix1[i][j]);
            matrix2[j][i] = matrix1[i][j];
        }
    }

    for (int i = 0; i < input_N; ++i) {
        for (int j = 0; j < input_M; ++j) {
            printf("%d%s", matrix2[i][j], (j == input_M - 1 ? "" : " "));
        }
        if (i != input_N - 1) { printf("
"); }
    }
    return 0;
}

image

上面是题目,一直是答案错误。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
#include <stdio.h>

int input_M = 0, input_N = 0;

int main() {
    int i = 0;
    int j = 0;
    scanf("%d%d", &input_M, &input_N);
    int matrix1[input_M][input_N];

    for (i = 0; i < input_M; ++i) {
        for (j = 0; j < input_N; ++j) {
            scanf("%d", &matrix1[i][j]);
        }
    }

    for (j = 0; j < input_N; ++j) {
        for (i = 0; i < input_M; ++i) {
            printf("%d", matrix1[i][j]);
            if (i == input_M - 1) {
                printf("
");
            } else {
                printf(" ");
            }
        }
    }
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...