APCS參考題解

資研社課練習

11/03

b965. 第 2 題 矩陣轉換

//函式

//旋轉 A -> B 
void A2B(int a[10][10], int b[10][10], int R, int C){
    for(int i = 0 ; i < C ; i++){
        for(int j = 0 ; j < R ; j++){
            b[i][j] = a[j][C - i - 1];
        }
    }
}

//旋轉 B -> A 
void B2A(int a[10][10], int b[10][10], int R, int C){
    for(int i = 0 ; i < R ; i++){
        for(int j = 0 ; j < C ; j++){
            a[i][j] = b[j][R - i -1];
        }
    }
}
//函式

//翻轉A
void FlipA(int a[10][10], int R, int C){
    for(int i = 0 ; i < R/2 ; i++){
        for(int j = 0 ; j < C ; j++){
            swap(a[i][j], a[R - 1 - i][j]);
        }
    }
}

//翻轉B
void FlipB(int b[10][10], int R, int C){
    for(int i = 0 ; i < C/2 ; i++){
        for(int j = 0 ; j < R ; j++){
            swap(b[i][j], b[C - i - 1][j]);
        }
    }
}

void Show(int s[10][10], int R, int C){
    for(int i = 0 ; i < R ; i++){
        for(int j = 0 ; j < C ; j++){
            if(j == C-1){
                printf("%d\n", s[i][j]);
                continue;
            }
            printf("%d ", s[i][j]);
        }
    }
}
int main(){
    int R, C, M;
    while(scanf("%d %d %d", &R, &C, &M) != EOF){
        int a[10][10] = {}, b[10][10] = {}, k[10] = {0};
        bool A = true, B = false;
        for(int i = 0 ; i < R ; i++){
            for(int j = 0 ; j < C ; j++){
                scanf("%d", &a[i][j]);
            }
        }
        for(int i = 0 ; i < M ; i++){
            scanf("%d", &k[i]);
        }

        for(int i = M-1 ; i >= 0 ; i--){
            if(k[i] == 0){
                if(A){
                    A2B(a, b, R, C);
                    A = false;
                    B = true;
                }
                else if(B){
                    B2A(a, b, R, C);
                    A = true;
                    B = false;
                }
            }
            else if(k[i] == 1){
                if(A){
                    FlipA(a, R, C);
                }
                else if(B){
                    FlipB(b, R, C);
                }
            }
        }

        if(A){
            printf("%d %d\n", R, C);
            Show(a, R, C);
        }
        else if(B){
            printf("%d %d\n", C, R);
            Show(b, C, R);
        }
    }
}

11/17

j605. 1. 程式考試

j606. 2. 造字程式

/*
https://zerojudge.tw/ShowProblem?problemid=j605
j605. 1. 程式考試
*/

#include <bits/stdc++.h>
using namespace std;

int main(){
    int k, t, s, max_t, max_s, error = 0;
    scanf("%d", &k);
    for(int i = 0 ; i < k ; i++){
        scanf("%d %d", &t, &s);
        if(i == 0){
            max_t = t;
            max_s = s;
        }
        if(max_s < s){
            max_t = t;
            max_s = s;
        }
        if(s == -1){
            error += 1;
        }
    }
    int ans = max_s - k - error * 2;
    if(ans < 0){
        ans = 0;
    }
    printf("%d %d", ans, max_t);
}
/*
https://zerojudge.tw/ShowProblem?problemid=j606
j606. 2. 造字程式
*/

#include <bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int K, Q, R;
    string str, a[25][25];
    cin >> K >> Q >> R >> str;
    for(int i = 0 ; i < K ; i++){
        a[0][i] = str[i];
    }
    for(int i = 1 ; i <= Q ; i++){
        for(int j = 0 ; j < K ; j++){
            int temp;
            cin >> temp;
            a[i][temp-1] = a[i-1][j];
        }
    }
    for(int i = 0 ; i < R ; i++){
        for(int j = 1 ; j <= Q ; j++){
            cout << a[j][i];
        }
        cout << "\n";
    }
}

資研社課練習 APCS參考題解

By d11130110周月蘅

資研社課練習 APCS參考題解

  • 136