2018年4月12日 星期四

計概15-06陣列-其他試題

【技藝競賽試題】

B01.下列程式執行後,sumk的值為何? (A)陣列索引超出範圍之錯誤 (B)48 4 (C)66 3 (D)44 2[96技競]

Dim a(4, 3)

a(1, 1) = 0: a(1, 2) = 3: a(1, 3) = 33

a(2, 1) = 0: a(2, 2) = 2: a(2, 3) = 22

a(3, 1) = 0: a(3, 2) = 1: a(3, 3) = 11

For i = 1 To 4

 For j = 1 To 3

  If a(i, j) <> 0 And a(i, j) Mod 2 <> 0 Then sum = sum + a(i, j): k = k + 1

 Next j

Next i

Print sum, k

符合a(i, j) <> 0 And a(i, j) Mod 2 <> 0a(1, 2) = 3a(1, 3) = 33a(3, 2) = 1a(3, 3) = 11sum = 3 + 33 + 1 + 11 = 48k = 4

 

C02.給一個8 * 20的二維陣列A,在以列(row)為主的排列方式儲存在記憶體內。若陣列第一個元素A(0, 0)儲放的位置為120,且陣列每個元素佔2個記憶體單位。請問A(4, 10)這個元素會被存放在哪一個位址? (A)100 (B)200 (C)300 (D)400[97技競]

A(0, 0)~A(0, 19)A(1, 0)~A(1, 19)A(2, 0)~A(2, 19)A(3, 0)~A(3, 19)A(4, 0)~A(4, 10)91個元素,A(4, 10)的位址在120 + 2 * (91 - 1) = 300

 

C03.下列程式執行前若陣列A(1)A(5)之值依序為2, 6, 5, 1, 3, 則其執行後印出陣列A(1)A(5)之結果為 (A)5, 6, 3, 2, 1 (B)1, 2, 3, 5, 6 (C)6, 5, 3, 2, 1 (D)5, 2, 6, 1, 3[97技競]

For J = 2 To 5

 t = A(J): I = J - 1

 M = J

 While (I >= 1) And (A(I) < t)

  If A(I) < t Then A(M) = A(I): I = I - 1: M = M - 1

 Wend

 A(I + 1) = t

Next J

J

t

I

M

 

J

t

I

M

 

2

6

1

2

A(2) = A(1) = 2

4

1

3

4

A(4) = 1

 

 

0

1

A(1) = t = 6

5

3

4

5

A(5) = A(4) = 1

3

5

2

3

A(3) = A(2) = 2

 

 

3

4

A(4) = A(3) = 2

 

 

1

2

A(2) = 5

 

 

2

3

A(3) = 3

 

C04.執行下列Visual Basic程式片段,請問最後a的值為何? (A)0 (B)1 (C)5 (D)10[98技競]

Dim X(10) As Integer

a = 0

For i = 10 To 1 Step -1

 X(i) = i Mod 2: a = a + X(i)

Next i

Print a

X(9) + X(7) + X(5) + X(3) + X(1) = 5

 

C05.VB中,宣告陣列Dim A(-5 To 1, -1 To 3) As Integer, Dim B(10) As Integer,其中AB陣列一共可儲存的元素共有多少個? (A)11 (B)34 (C)46 (D)45[100技競]

A陣列(1- (-5) + 1)*(3- (-1) + 1) +B陣列11 = 46

 

B06.執行下列BASIC程式,其結果為何? (A)0 (B)140 (C)156 (D)306[100技競]

Dim A(4, 4) As Integer

Dim i, j As Integer

For i = 1 To 4

 For j = 0 To 4

  A(i, j) = 2 * i + 4 * j

 Next j

Next i

Print A(1, 3) * A(3, 1)

A(1, 3) * A(3, 1) = (2 + 12) * (6 + 4) = 140

 

D07.下列BASIC程式之執行結果為何? (A)0 (B)1 (C)2 (D)3[100技競]

Dim A(10) As Integer

Dim I As Integer

For K = 1 To 10

 A(K) = (K ^ 2 + 1) Mod 8

Next K

Print(A(4) + A(7))

A(4) + A(7) = (4 ^ 2 + 1) Mod 8 + (7 ^ 2 + 1) Mod 8 = 1 + 2 = 3

 

C08.執行下列BASIC程式,其A(4)結果為何? (A)3 (B)6 (C)10 (D)15[101技競]

Dim A(5), I, SUM As Integer

SUM = 0

For I = 1 To 5

 SUM = SUM + I

 A(I) = SUM

Next I

A(4) = 1 + 2 + 3 + 4 = 10

 

C09.執行以下BASIC程式,其A(3, 3)結果為何? (A)11 (B)24 (C)26 (D)15[101技競]

Dim A(3, 3)

For i = 0 To 3

 For j = 0 To 3

  If i >= 1 And j >= 1 Then

   A(i, j) = (i * j) + i + j + A(i - 1, j - 1)

  Else

   A(i, j) = (i * j) + i + j

  End If

 Next

Next

A(0, 0) = (0 * 0) + 0 + 0 + A(0, 0) = 0

A(1, 1) = (1 * 1) + 1 + 1 + A(0, 0) = 3

A(2, 2) = (2 * 2) + 2 + 2 + A(1, 1) = 11

A(3, 3) = (3 * 3) + 3 + 3 + A(2, 2) = 26

 

C10.執行下列BASIC程式,其A(4)結果為何? (A)1 (B)3 (C)7 (D)12[101技競]

Dim A(10) As Integer

Dim K As Integer

K = 1

While K <= 5

 If K = 1 Or K = 2 Then

  A(K) = 1

 Else

  A(K) = (K ^ 2 + 1) Mod 7 + A(K - 1)

 End If

 K = K + 1

End While

K

A(K)

K

A(K) = (K ^ 2 + 1) Mod 7 + A(K - 1)

1

A(1) = 1

3

A(3) = (3 ^ 2 + 1) Mod 7 + A(3 - 1) = 3 + 1 = 4

2

A(2) = 1

4

A(4) = (4 ^ 2 + 1) Mod 7 + A(4 - 1) = 3 + 4 = 7

 

【其他試題】

C01.Dim A%(3, 3)所宣告的二維陣列,共占用記憶體多少個位元組? (A)6 (B)9 (C)32 (D)以上皆非。

%表示整數,每個整數占2Bytes

陣列元素編號從0開始,(3 + 1) * (3 + 1) = 16個元素,共占32Bytes

 

B02.BASIC語言中,若一個二維陣列宣告為DIM TBL(5, 8),則此二維陣列包含的元素個數是 (A)13 (B)40 (C)54 (D)60[85殘障四等]

陣列索引的下限值為0(5 + 1) * (8 + 1) = 54個元素

Option Base 1,陣列索引的下限值為15 * 8 = 40個元素

 

D03.BASIC程式語言中宣告Dim A!(3, 3),佔用多少位元組的記憶體位址? (A)9 (B)16 (C)36 (D)64[85商業中夜]

!表示單精數,每個單精數占4Bytes,有16個元素,共占64Bytes

 

A04.Basic中,若未設定OPTION BASE,則DIM A(3, 4)敘述會在記憶體中預留多少個陣列元素的存放空間? (A)20 (B)12 (C)9 (D)7個。[89工設北夜]

(3 + 1) * (4 + 1) = 20

 

B05.陣列(Array)的註標(index)通常是 (A)實數 (B)整數 (C)小數 (D)分數。[90教育視導]

 

【綜合題】

【題目1】下列程式執行結果為何?

Dim A(5) As Integer

A(1) = 0: A(2) = 1

For K = 3 To 5

 A(K) = K + A(K - 1) - A(K - 2)

 Print K, A(K)

Next K

Print A(5)

K = 3 A(3) = 3 + A(3 - 1) - A(3 - 2) = 3 + A(2) - A(1) = 3 + 1 - 0 = 4

K = 4 A(4) = 4 + A(4 - 1) - A(4 - 2) = 4 + A(3) - A(2) = 4 + 4 - 1 = 7

K = 5 A(5) = 5 + A(5 - 1) - A(5 - 2) = 5 + A(4) - A(3) = 5 + 7 - 4 = 8 答案

 

【題目2】下列程式執行結果為何?

Dim A(4) As Integer

A(1) = 0

For K = 2 To 4

 If K Mod 2 = 1 Then

  A(K) = A(K - 1) + 1

 Else

  A(K) = A(K - 1) + 2

 End If

Next K

Print A(4)

K = 2 A(2) = A(2 - 1) + 2 = A(1) + 2 = 0 + 2 = 2

K = 3 A(3) = A(3 - 1) + 1 = A(2) + 1 = 2 + 1 = 3

K = 4 A(4) = A(4 - 1) + 2 = A(3) + 2 = 3 + 2 = 5 答案

 

【題目3】執行下列Visual Basic程式片段其輸出結果為

Dim a(5, 5) As Integer

For i = 1 To 5

 For j = 1 To 5

  a(j, i) = i + j

 Next j

Next i

For i = 1 To 5

 For j = 1 To 5

  If a(i, j) Mod 3 = 0 then c = c + 1: s = s + a(i, j)

 Next j

Next i

Print c; s

第一段執行流程如下:

i

j

a(j, i)

i

j

a(j, i)

1

1~5

2~6

4

1~5

5~9

2

1~5

3~7

5

1~5

6~10

3

1~5

4~8

 

 

 

第二段符合a(i, j) Mod 3 = 0條件時,執行c = c + 1: s = s + a(i, j),共有9個組合,總額為54

i

j

a(j, i)

i

j

a(j, i)

1

2

3

4

2

6

5

6

5

9

2

1

3

5

1

6

4

6

4

9

3

3

6

 

 

 

 

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。