---
# System prepended metadata

title: APCS 2021/01/09 - A卷
tags: [筆試題目, 2021 觀念題]

---

APCS 2021/01/09 - A卷
===
###### tags: `2021 觀念題` `筆試題目`

```c=
int func ( int a[] , int s , int f) {
    int min = 0, max = s - 1 ;
    while ( min < max ){
        int mid = (min + max) / 2;
        if ( f > a[mid] ){
            min = mid + 1;
        } else if ( f < a[mid] ){
            max = mid - 1;
        } else {
            return mid;
        }
    }
    return -1;
}
int main(){
    int a[8] = { 2, 3, 3, 5, 8, 8, 8, 8 } ;
    printf("%d", func(a, 8, 8) ) ;
}
```
01. 上述輸出的答案為下列何者？
    + [A] 4
    + [B] 5
    + [C] 6
    + [D] 7

---
```c=
int main(){
    char c = 'a';
    switch(c){
        case 'a':
            printf("a");
        case 'b':
            printf("b");
        case 'c':
            printf("c");
        default:
            printf("d");
    }
}
```
02. 上述輸出的答案為下列何者？
    + [A] a
    + [B] abc
    + [C] ad
    + [D] abcd

---
```c=
int level = 0;
int num[6] ;
for (int i = 1 ; i <= 6 ; i = i + 1 ) {
    if ( level == 0 ) {
        num[level] = i ;
        level = level + 1 ;
    } else if ( rand() % 2 == 0 ){ 
        level = level - 1 ;
        printf("%d ", num[level] ) ;
        i = i - 1;
    } else {
        num[level] = i ;
        level = level + 1 ;
    }
}
while ( level ) {
    level = level - 1 ;
    printf("%d ", num[level] ) ;
}
```
03. rand() 是一個會回傳隨機整數的函數，試問下列選項何者是無法達成？
    + [A] 1 2 3 5 6 4
    + [B] 4 6 5 3 1 2
    + [C] 3 4 6 5 2 1
    + [D] 6 5 4 3 2 1

---
```c=
int f(){
    int a[5] = { 9, 2, 4, 7, 3 } ;
    int b[10] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1} ;
    int c = 0;
    for (int i = 0; i < 5 ; i = i + 1 ) {
        c += b[ a[i] ];
    }
    return c;
}
```
04. 回傳的 c 值為多少？
    + [A] 1
    + [B] 2
    + [C] 3
    + [D] 4

---
```c=
int f ( int n ){
    if ( n < 2 )
        return n ;    
    else
        return n + f(__________) ;
}
```
05. 執行上述的函數時回傳的結果： $f$(14) = 28 ，$f$(10) = 21 ， $f$(6) = 12，判斷式該如何選擇才能符合對應關係？
    + [A] $\frac{n+1}{2}$
    + [B] $\frac{n}{2}$
    + [C] $\frac{n-1}{2}$
    + [D] $\frac{n}{2}+1$

---
```c=
int f ( int n ){
    int a[5] = { 5, 3, 10, 4, 20 } ;
    int b[5] = {} ;
    int size = 0 ;
    for ( int i = 0 ; i < 5; i = i + 1 ) { 
        b[ size ] = a[i] ;
        size =  size + 1 ;
    }
    while ( size ) {
        printf("%d ", b[ size - 1]);
        size =  size - 1 ;
    }
}
```
06. 執行上述的函數時後輸出的結果？
    + [A] 3 4 5 10 20
    + [B] 5 3 10 4 20
    + [C] 20 10 5 4 3
    + [D] 20 4 10 3 5

---
```c=
for (int i = 0; i < 6 ; i += 1 ){
    for (int j = 0 ; __________ ; j += 3 ){
        printf("[%d]", i+j);
    }
}
```
07. 執行為上述的程式碼後輸出結果為[1][2][3][4][7][5][8]，試問判斷是該如何選擇？
    + [A] $j < i$
    + [B] $j \leq i$
    + [C] $j > i$
    + [D] $j \geq i$

---
```c=
void reverse( int a[], int n ) {
    for (int i = 0 ; __________ ; i += 1 ) {    
        int temp = a[i] ;
        a[i] = a[ n-1-i ] ;
        a[ n-1-i ] = temp ;
    }
}
```
08. 為了讓陣列反向儲存時，判斷式應該如何選擇？
    + [A] $i < n$
    + [B] $i < \frac{n}{2}$
    + [C] $i \leq n$
    + [D] $i \leq \frac{n}{2}+1$

---
```c=
#define SIZE 7
int main() {
    int outer = 0 , sum = 0 , index = 0 ;
    int a[7] = { 2, 1, 3, 4, 5, 0, 6 } ;
    while( outer < SIZE ){ 
        for( index = 0 ; index < a[outer] ; index += 1 )
            if( index > 5 )
                break ;
            else if ( a[ index ] >= a[ index + 1 ] ){
                sum = sum + a[ index ] ;
                a[ index ] = a[ index + 1 ] ;
            }
        outer = outer + sum ;
    }
}
```
09. 執行完上述的程式碼後 sum 的值應該為多少？
    + [A] 0
    + [B] 2
    + [C] 3
    + [D] 6

---
```c=
void f(int x, int y){
    int temp = x;
    x = y;
    y = temp;
}
int main(){
    int x = 2, y = 5 ;
    f(x, y) ;
    printf("%d", (x - y) * (x + y + 4) );
}
```
10. 輸出數值為多少？
    + [A] 33
    + [B] 1
    + [C] -1
    + [D] -33

---
```c=
int f(int *a, int *b) ;
int g(int *a, int *b) ;

int main(){
    int a = 32, b = 5 ;
    int x = f(&a, &b) ;
    printf("%d %d %d", x, a, b) ;
}
int f(int *a, int *b ) {
    int c = 10 ;
    *a = *a + *b / 5 ; 
    int p = g(b, a) % 2;
    if (p % 2 == 0){
        return *b - p ;
    } else {
        return *b + p ;
    }
}
int g(int *a ,int *b ) {
    return ( *a - *b ) * 5 + 2 ;
}
```
11. 輸出數值為多少？
    + [A] 5 33 5
    + [B] 5 32 5
    + [C]143 33 5
    + [D]143 32 5

---
```c=
int f(char s[], int n){
    char t[1000] ;
    int j = 0 ;
    for (int i = 0 ; i < strlen(s) ; i += 1, j += 2) {
        if (s[ i ] == '0'){
            t[ j ] = 'w' ;
            t[j+1] = 'a' ;
        } else if ( _____ || _____ ) {
            t[ j ] = 't' ;
            t[j+1] = 'e' ;
        } else if (s[ i ] == '3' || s[ i ] == '5'){
            t[ j ] = 'd' ;
            t[j+1] = 'o' ;
        } else if (s[ i ] == '4' || s[ i ] == '6'){
            t[ j ] = 'p' ;
            t[j+1] = 'u' ;
        } else {
            t[ j ] = ' ';
            j = j - 2 ;
        }
    }
    t[j] = '\0';
    printf("%s\n", t);
}
int main(){
    char s[] = "0328675";
    f(s, 8);
}
```
12. 輸出"wadoteputedo"時，判斷式應該如何選擇才符合題目要求？
  原版是給定 3 個數字字串對應的英文字串...
    + [A] s[i] == '2' || s[i] == '7'
    + [B] s[i] == '2' || s[i] == '8'
    + [C] s[i] == '1' || s[i] == '8'
    + [D] s[i] == '1' || s[i] == '2'
    
---
```c=
int main(){
    char s[] = "3*5/6+2-1" ;
    char a[10] = {} ;
    char b[10] = {} ;
    int la = 0, lb = 0 ;
    for ( int i = 0 ; i < strlen(s) ; i += 1 ){
        switch( s[i] ){
            case '+': 
            case '-': 
            case '*': 
            case '/':
                a[la] = s[i] ;
                la = la + 1 ;
                break ;
            default : 
                b[lb] = s[i] ;
                lb = lb + 1 ;
        }
    }
    while( la ){
        la = la - 1 ;
        b[ lb ] = a[ la ] ;
        lb = lb + 1 ;
    }
    b[lb] = '\0' ;
    printf("%s\n",b ) ;
}

```
13. 輸出的字串為何？
    + [A] 35621*/+ -
    + [B] */+ -35621
    + [C] 35621- +/*
    + [D] *35/9+2-1

---
```c=
int main(){
    int n ;
    scanf("%d", &n) ;
    int mat[n][n] = {};
    int x = n-1 , y = (n - 1) / 2 ;
    for(int i = 1 ; i <= n * n ; i = i + 1 ){
        int a = _____ ;
        int b = _____ ;
        if ( mat[a][b] ){
            a = ( x + 1 )% n;
            b = y ;
        }
        x = a ;
        y = b ;
        mat[x][y] = i ;
    }
}
```
14. 邊長Ｎ為奇數的魔方陣，依照下列規則填入１到 $N^{2}$ 的數字就能保證每一行和每一列以及斜對角的數字總和都相同。
(1) 由最下面那一列的中間格開始填入1
(2) 接續格在前一格的左下方，除非超出邊界或格內已填數字
(3) 超出下邊界，則接續格改為左邊一行的最上面一列，除非超出左邊界如說明(6)
(4) 超出左邊界，則接續格改為下面一列的最右邊一行，除非超出下邊界如說明(6)
(5) 若接續格已填有數字，接續格改為原格上方一格
(6) 若下面及左邊同時超出邊界時，接續格改為原格的上方一格
    + [A] (x-1+n)%n , (y+1+n)%n
    + [B] (x-1+n)%n , (y-1+n)%n
    + [C] (x+1+n)%n , (y+1+n)%n
    + [D] (x+1+n)%n , (y-1+n)%n
