#46029: (C語言)有試過測資正確 但實際解答有誤


frcmoonfall@gmail.com (玥楓)

學校 : 新北市立中和高級中學
編號 : 291095
來源 : [36.231.95.157]
最後登入時間 :
2025-05-10 17:01:17
b965. 2. 矩陣轉換 -- 2016年3月apcs | From: [36.231.95.157] | 發表日期 : 2025-05-10 17:16

以下有包含除錯,用//隱藏,刪除部分//可輸出每一步驟

可能有一些無意義語句。

 

//APCS2.矩陣轉換
//矩陣=二維陣列
//Zerojudge提交答案版本
#include <stdio.h>
#include <stdlib.h>

int main(){
    int R=0,C=0,M=0,A[100][100],B[100][100],MK[100],KM[100];
    int i=0,j=0,k=0;
    int a=0,b=0;
    int x=0,y=0;
    int count;
    int turn=0;
    int r,c,change;
    int mk=0;
    //printf("輸入R、C、M三個整數\n");
    scanf("%d %d %d",&R,&C,&M);
    //printf("輸入R行,每行C個整數\n");
    //printf("第i行第j個數字代表陣列Bij\n");
    for(j=0;j<R;j++){
        for(i=0;i<C;i++){
        scanf("%d",&B[i][j]);
        A[i][j] = B[i][j];
        }
    }
    //printf("有一行M個整數代表對A進行的操作\n");
    j = M - 1;
    for(i=0;i<M;i++){
        scanf("%d",&MK[i]);
        KM[j] = MK[i];
        j = j - 1;
    }
        count = 0;
        turn = count % 2;
        if(turn == 0){
            r = R;c = C;
        }else if(turn == 1){
            r = C;c = R;}
    for(k=0;k<M;k++){
    mk = KM[k];
    //printf("執行%d操作\n",mk);
    a = 0;b = 0;x = 0;y = 0;
//旋轉
    if(mk == 0){
        for(j=0;j<r;j++){
            for(i=c-1;i>=0;i--){
                A[x][y] = B[i][j];
                y = y + 1;
            }
            x = x + 1;
            y = 0;
        }
    count = count + 1;

    change = r;
    r = c;
    c = change;
    turn = turn + 1;
    if(turn >= 2){
        turn = turn - 2;
    }
    }
//翻轉-旋轉零或偶數次
    if(mk == 1){
        if(turn >= 2){
            turn = turn - 2;
        }
            //printf("%d",turn);
        if(turn == 0){
        for(i=0;i<R;i++){
            for(j=C;j>=0;j--){
                A[a][b] = B[i][j];
                b = b+1;
            }
            b = 0;
            a = a+1;
        }
        }else if(turn == 1){
            a=0;b=0;
            for(i=0;i<r;i++){
                for(j=c-1;j>=0;j--){
                    A[a][b] = B[i][j];//Array[橫排][直列]
                    b = b+1;
                }
                b = 0;
                a = a+1;
            }
        }
    }

//奇數次旋轉後翻轉會出錯
//目前改法 將翻轉分開寫 旋轉奇偶次時適用情況 用if-else

//調整奇數次旋轉時的翻轉
//讓B等於新的A再進行操作
    for(i=0;i<R+C;i++){
        for(j=0;j<R+C;j++){
            B[i][j] = A[i][j];
        }
    }
    //暫時刪掉中括號之後補回來
}//就這個括號
    //printf("旋轉%d次\n",count);
//輸出
    printf("%d %d\n",r,c);
//A矩陣
    for(i=0;i<r;i++){
        for(j=0;j<c;j++){
            printf("%d",A[j][i]);
            if(j != c-1){
                printf(" ");
            }
        }
        printf("\n");
    }

    //除錯測試過程的中括號,刪掉了
    return 0;
}

 
ZeroJudge Forum