1. 程式人生 > 其它 >[基礎]2019年CSP-J初賽試題(普及組)試題詳解 3/3

[基礎]2019年CSP-J初賽試題(普及組)試題詳解 3/3

三、完善程式(單選題,每小題3分,共計30分)

1.(矩陣變幻)有一個奇幻的矩陣,在不停的變幻,其變幻方式為:數字0

變成矩陣$\begin{bmatrix} 0&0 \ 0&1 \ \end{bmatrix}$,數字1變成

矩陣$\begin{bmatrix} 1&1 \ 1&0 \ \end{bmatrix}$最初該矩陣只有

一個元素0,變幻n次後,矩陣會變成什麼樣?

例如,矩陣最初為:$\begin{bmatrix} 0 \ \end{bmatrix}$;矩陣變幻1

次後:$\begin{bmatrix} 0&0 \ 0&1 \ \end{bmatrix}$矩陣變幻2次

後:$\begin{bmatrix} 0&0&0&0 \ 0&1&0&1 \ 0&0&1&1 \ 0&1&1&0 \end{bmatrix}$
輸入一行一個不超過10的正整數n。輸出變幻n次後的矩

陣。 試補全程式。

提示:

"<<"表示二進位制左移運算子,例如

$(11)_2 << 2 = (1100)_2(11)2​<<2=(1100)2​;$

而“^”表示二進位制異或運算子,它將兩個參與運算的數中的每個對應的

二進位制位—進行比較,若兩個二進位制位相同,則運算結果的對應二進位制

位為0,反之為1。

#include <cstdio>
using namespace std;
int n;
const int max_size = 1 << 10;

int res[max_size][max_size];

void recursive(int x, int y, int n, int t) {
    if (n == 0) {
        res[x][y] = ①;
        return;
    }
    int step = 1 << (n - 1);
    recursive(②, n - 1, t);
    recursive(x, y + step, n - 1, t);
    recursive(x + step, y, n - 1, t);
    recursive(③, n - 1, !t);
}

int main() {
    scanf("%d", &n);
    recursive(0, 0, ④);
    int size = ⑤;
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++)
            printf("%d", res[i][j]);
        puts("");
    }
    return 0;
}   

①處應填()

A. n%2 B. 0 C. t D. 1

②處應填()

A. x-step,y-step B. X,y-step C. x-step,y D.x,y

③處應填()

A. x-step,y-step B. x+step,y+step C. x-step,y D. X,y-step

④處應填()

A. n-1,n%2 B. n,0 C. n,n%2 D. n-1,0

⑤處應填()

A. 1<<(n+1) B. 1<<n C. n+1 D. 1<<(n-1)

【答案】CDBBB

【解析】

(計數排序)計數排序是一個廣泛使用的排序方法。下面的程式使用雙

關鍵字計數排序,將n對10000以內的整數,從小到大排序。例如有三對

整數(3,4)(3,4)、(2,4)(2,4)、(3,3)(3,3),那麼排序之後應該是

(2,4)(2,4)、(3,3)(3,3)、(3,4)(3,4) 。輸入第一行為nn,接下來

nn行,第ii行有兩個數a[i]a[i]和b[i]b[i],分別表示第ii對整數的

第一關鍵字和第二關鍵字。從小到大排序後輸出。資料範圍

$1<n<10^7$
且$1<a[i],b[i]<10^4$

提示:應先對第二關鍵字排序,再對第一關鍵字排序。陣列ord[]儲存

第二關鍵字排序的結果,陣列res[]儲存雙關鍵字排序的結果。

試補全程式。

#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 10000000;
const int maxs = 10000;

int n;
unsigned a[maxn], b[maxn],res[maxn], ord[maxn];
unsigned cnt[maxs + 1];
int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) 
        scanf("%d%d", &a[i], &b[i]);
    memset(cnt, 0, sizeof(cnt));
    for (int i = 0; i < maxs; ++i)
        ①; // 利用 cnt 陣列統計數量
    for (int i = 0; i < n; ++i) 
        cnt[i + 1] += cnt[i];
    for (int i = 0; i < n; ++i)
        ②; // 記錄初步排序結果
    memset(cnt, 0, sizeof(cnt));
    for (int i = 0; i < n; ++i)
        ③; // 利用 cnt 陣列統計數量
    for (int i = 0; i < maxs; ++i)
        cnt[i + 1] += cnt[i];
    for (int i = n - 1; i >= 0; --i)
        ④ // 記錄最終排序結果
    for (int i = 0; i < n; i++)
        printf("%d %d", ⑤);

    return 0;
}

①處應填()

A. ++cnt [i]

B. ++cnt[b[i]]

C. ++cnt[a[i] * maxs + b[i]]

D. ++cnt[a[i]]

②處應填()

A. ord[--cnt[a[i]]] = i

B. ord[--cnt[b[i]]] = a[i]

C. ord[--cnt[a[i]]] = b[i]

D. ord[--cnt[b[i]]] = i

③處應填()

A. ++cnt[b[i]]

B. ++cnt[a[i] * maxs + b[i]]

C. ++cnt[a[i]]

D. ++cnt [i]

④處應填()

A. res[--cnt[a[ord[i]]]] = ord[i]

B. res[--cnt[b[ord[i]]]] = ord[i]

C. res[--cnt[b[i]]] = ord[i]

D. res[--cnt[a[i]]] = ord[i]

⑤處應填()

A. a[i], b[i]

B. a[res[i]], b[res[i]]

C. a[ord[res[i]]]j b[ord[res[i]]]

D. a[res[ord[i]]]j b[res[ord[i]]]

【答案】BDCAB