#28382: 請問我這樣的寫法哪裡有錯?


ic231542211 (willie)


#include<stdio.h>

#include<string.h>

int main(){

    int test, n, a = 0;

    int negative = 0, symmetric = 1;

    scanf("%d\n", &test);

    for(int i = 0; i < test; i++){

        scanf("N = %d\n", &n);

        long long int matrix[101][101];

        memset(matrix, 0, sizeof(matrix));

        for(int i = 0; i < n; i++){

            for(int j = 0; j < n; j++){

                scanf("%lld", &matrix[i][j]);

                //找負數值

                if(matrix[i][j] < 0){

                    negative = 1;

                }

            }

        }

        //找是否對稱

        for(int i = 0; i < n; i++){

            for(int j = 0; j < n; j++){

                if(matrix[i][j] != matrix[n - 1 - i][n - 1 - j]){

                    symmetric = 0;

                }

            }

        }

        if(negative == 1 || symmetric == 0){

            printf("Test #%d: Non-symmetric.\n", ++a);

        }

        else{

            printf("Test #%d: Symmetric.\n", ++a);

        }

    }

}

請問為何當我有兩筆測資時,為何輸入第二筆的測資會造成cmd視窗馬上關掉?

#28383: Re:請問我這樣的寫法哪裡有錯?


linlincaleb@gmail.com (臨末之頌)


#include

#include

int main(){

    int test, n, a = 0;

    int negative = 0, symmetric = 1;

    scanf("%d\n", &test);

    for(int i = 0; i < test; i++){

        scanf("N = %d\n", &n);

        long long int matrix[101][101];

        memset(matrix, 0, sizeof(matrix));

        for(int i = 0; i < n; i++){

            for(int j = 0; j < n; j++){

                scanf("%lld", &matrix[i][j]);

                //找負數值

                if(matrix[i][j] < 0){

                    negative = 1;

                }

            }

        }

        //找是否對稱

        for(int i = 0; i < n; i++){

            for(int j = 0; j < n; j++){

                if(matrix[i][j] != matrix[n - 1 - i][n - 1 - j]){

                    symmetric = 0;

                }

            }

        }

        if(negative == 1 || symmetric == 0){

            printf("Test #%d: Non-symmetric.\n", ++a);

        }

        else{

            printf("Test #%d: Symmetric.\n", ++a);

        }

    }

}

請問為何當我有兩筆測資時,為何輸入第二筆的測資會造成cmd視窗馬上關掉?

檢查你的變數i

#28391: Re:請問我這樣的寫法哪裡有錯?


cges30901 (cges30901)


檢查你的變數i


變數i看起來沒問題啊,只是shadow而已吧?

#28392: Re:請問我這樣的寫法哪裡有錯?


cges30901 (cges30901)


        scanf("N = %d\n", &n);


                scanf("%lld", &matrix[i][j]);


請問為何當我有兩筆測資時,為何輸入第二筆的測資會造成cmd視窗馬上關掉?


問題在於第二次用scanf輸入"N = %d\n"前,由於上一次的scanf輸入matrix[i][j],後面有換行符號還留在stdin裡面,導致輸入"N = %d\n"會失敗。其中一個解決方式是在輸出結果後面加一個getchar(),把換行符號消耗掉。

#28393: Re:請問我這樣的寫法哪裡有錯?


cges30901 (cges30901)


    int negative = 0, symmetric = 1;


還有另一個問題,你的negative和symmetric在不同筆測資之間沒有重設

#28395: Re:請問我這樣的寫法哪裡有錯?


linlincaleb@gmail.com (臨末之頌)


    int negative = 0, symmetric = 1;


還有另一個問題,你的negative和symmetric在不同筆測資之間沒有重設

對耶 我看錯 抱歉

#28397: Re:請問我這樣的寫法哪裡有錯?


ic231542211 (willie)


    int negative = 0, symmetric = 1;


還有另一個問題,你的negative和symmetric在不同筆測資之間沒有重設

對耶 我看錯 抱歉


謝謝你們,我搞定了