第1題
針對以下Java 程式,執行後輸出為何?(25 分)
public class Count {
public static int A(int x) {
if (x < 20) x = A(x+5) + B(x);
return x;
}
public static int B(int x) {
System.out.print("*");
return 3 * x;
}
public static int C(int x) {
System.out.print("*");
return 3 * A(x-3);
}
public static boolean D(boolean x, boolean y) {
return ((x || !y) || (x && y));
}
public static boolean E(boolean x, boolean y) {
return (!x && (x && y));
}
public static void main(String[] args) {
System.out.println(A(3));
//
(I)
System.out.println(B(3));
//
(II)
System.out.println(C(3));
//
(III)
System.out.println(D(true, true));
//
(IV)
System.out.println(E(true, false));
//
(V)
}
}
第3題
針對以下C 程式,其輸出為下列表格,說明compress 函式的功能,並完
成程式碼(I)、(II)、(III)、(IV),使之執行正確。(25 分)
程式輸出
2 4
6 5
2 4 6
6 5 4
1 4 8
3 5
4 6
#include <stdio.h>
#define SIZE 10
int op(int data[][SIZE], int x, int y, int n) {
int value = 0;
for (int i = x; i < x+n; i++)
for (int j = y; j < y+n; j++)
value = value + data[i][j];
return value/(n*n);
}
void print(int target[][SIZE], int size) {
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++)
printf("%d ", target[x][y]);
printf("\n");
}
}
void compress(int data[][SIZE], int
(I)
[][SIZE],
int size, int n) {
int cSize =
(II)
;
for (int x=0; x< cSize; x++)
for (int y=0; y<cSize; y++)
target
(III)
=op(data, x*n, y*n, n);
(IV)
(target, cSize);
}
int main() {
int data[][SIZE] = {{1, 2, 3, 4, 5, 6},
{3, 4, 5, 6, 7, 8},
{5, 6, 1, 2, 3, 4},
{7, 7, 9, 9, 5, 5},
{2, 2, 4, 4, 8, 8},
{1, 1, 5, 5, 9, 9}};
int target[SIZE][SIZE];
compress(data, target, 4, 2);
compress(data, target, 6, 2);
compress(data, target, 6, 3);
return 0;
}
第4題
針對以下C#程式,其輸出為80, 85,完成程式碼(I)、(II)、(III)、(IV)、
(V),使之執行正確。(25 分)
public
(I)
class Score {
protected int [] data;
protected int value;
public Score(int [] d) {
data = new int[d.Length];
value = 0;
for (int i = 0; i < d.Length; i++)
data[i] = d[i];
}
private void print() {Console.WriteLine(""+value); }
protected abstract void compute(int[] weight);
public void getValue(int[] weight) {
compute(weight);
(II)
();
}
}
public class WeightScore:Score {
public WeightScore(int [] data):
(III)
(data) {}
protected
(IV)
void compute(int [] weight) {
int totalWeight = 0;
for (int i = 0; i < data.Length; i++) {
value = value + data[i] * weight[i];
totalWeight = totalWeight + weight[i];
}
value = value /
(V)
;
}
}
public class Test {
static void Main(string[] args) {
int[] data1 = {90, 80, 70, 85};
int[] data2 = {90, 80, 80, 85};
int[] weight1 = {1, 2, 3, 4};
int[] weight2 = {4, 2, 1, 3};
Score x = new WeightScore(data1);
Score y = new WeightScore(data2);
x.getValue(weight1);
y.getValue(weight2);
}
}