본문 바로가기
자바(Java)/자바 AP

AP Computer Science A [FRQ] Practice (5)

by 긱펀 2024. 4. 4.
반응형

AP Computer Science A [FRQ] Practice (5)

 

-링크: https://runestone.academy/ns/books/published/csawesome/Unit8-2DArray/freeResponse.html?mode=browsing

 

 

2012 (4)번 문제

(아래 "더보기" 클릭)

더보기

2012 (4)번

 

 

 

 


 

2012 (4)번 문제 정답 (아래 "더보기")

더보기

2012 (4)번 문제 정답

(a)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Part (a):
public int countWhitePixels() {
    int whitePixelCount = 0;
    for (int[] row : this.pixelValues) {
        for (int pv : row) {
            if (pv == this.WHITE) {
                whitePixelCount++;
            }
        }
    }
    return whitePixelCount;
}
 
// Part (a): Alternative solution
public int countWhitePixels() {
    int whitePixelCount = 0;
    for (int row = 0; row < pixelValues.length; row++) {
        for (int col = 0; col < pixelValues[0].length; col++) {
            if (pixelValues[row][col] == WHITE) {
                whitePixelCount++;
            }
        }
    }
    return whitePixelCount;
cs

 

(b)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Part (b):
public void processImage() {
    for (int row = 0; row < this.pixelValues.length-2; row++) {
        for (int col = 0; col < this.pixelValues[0].length-2; col++) {
            this.pixelValues[row][col] -= this.pixelValues[row+2][col+2];
            if (this.pixelValues[row][col] < BLACK) {
                this.pixelValues[row][col] = BLACK;
            }
        }
    }
}
 
// Part (b): Alternative solution
public void processImage() {
    for (int row = 0; row < this.pixelValues.length; row++) {
        for (int col = 0; col < this.pixelValues[0].length; col++) {
            if (row + 2 < pixelValues.length && col + 2 < pixelValues[row].length) {
                this.pixelValues[row][col] -= this.pixelValues[row+2][col+2];
                if (this.pixelValues[row][col] < BLACK) {
                    this.pixelValues[row][col] = BLACK;
                }
            }
        }
    }
}
cs

 

 


 

2011년 (4)번 문제

(아래 "더보기"클릭)

더보기

2011년 (4)번 문제

 

 

 


 

 

2011년 (4)번 정답(아래 "더보기" 클릭)

더보기

2011년 (4)번 정답

(a)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Part (a):
private void fillBlock(String str) {
    int pos = 0;
    for (int r = 0; r < this.numRows; r++ ) {
        for (int c = 0; c < this.numCols; c++ ) {
            if (pos < str.length()) {
                this.letterBlock[r][c] = str.substring(pos, pos+1);
                pos++;
            } 
            else {
                this.letterBlock[r][c] = "A";
             }
        }
    }
}
 
// Alternative solution
private void fillBlock(String str) {
    for (int r = 0; r < this.numRows; r++ ) {
        for (int c = 0; c < this.numCols; c++ ){
            if (str.length() > (c + (r * this.numCols))) {
                this.letterBlock[r][c] = str.substring(c + r * this.numCols, 1 + c + r * this.numCols);
            } 
            else {
                this.letterBlock[r][c] = "A";
            }
        }
    }
cs

 

(b)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Part (b):
public String encryptMessage(String message) {
    String encryptedMessage = "";
    int chunkSize = this.numRows * this.numCols;
    while (message.length() > 0) {
        if (chunkSize > message.length()) {
            chunkSize = message.length();
        }
        fillBlock(message);
        encryptedMessage += encryptBlock();
        message = message.substring(chunkSize);
    }
    return encryptedMessage;
}
 
// Alternative solution
public String encryptMessage(String message) {
    if (message.length() == 0return "";
    
    fillBlock(message);
    
    if (message.length() <= this.numRows * this.numCols) {
        return encryptBlock();
    }
    return (encryptBlock() + encryptMessage(message.substring(this.numRows * this.numCols)));
}
cs

 


 

728x90
반응형

댓글