자격증/정보처리기사 실기 - 기출문제

[정보처리기사 실기] 2022년 3회 01 - 프로그래밍 [C]

쏠솔랄라 2025. 7. 23. 15:59

 

 

 

01 다음 C 언어로 구현된 프로그램을 분석하여 배열 <mines>의 각 칸에 들어갈 값을 쓰시오.

#include <stdio.h>
main() {
	int field[4][4] = { {0, 1, 0, 1}, {0, 0, 0, 1}, {1, 1, 1, 0}, {0, 1, 1, 1} };
	int mines[4][4] = { {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} };
	int w=4, h=4;
	for(int y=0; y<h; y++) {
		for(int x=0; x<w; x++) {
			if(field[y][x]==0) continue;
			for(int j=y-1; j<=y+1; j++) {
				for(int i=x-1; i<=x+1; i++) {
					if(chkover(w, h, j, i) == 1)
						mines[j][i] +=1;		
				}
			}
 		}
	}
}
int chkover(int w, int h, int j, int i) {
	if(i>=0 && i<w && j>=0 && j<h) return 1;
	return 0;
}

 

배열 <field>

0 1 0 1
0 0 0 1
1 1 1 0
0 1 1 1

 

배열 <mines>

       
       
       
       

 

 

 

더보기
1 1 3 2
3 4 5 3
3 5 6 4
3 5 5 3

 

 

 

해설

  코드   실행순서 및 해석
1 #include <stdio.h>    
2 main() {    
3       int field[4][4] = { {0, 1, 0, 1}, {0, 0, 0, 1}, {1, 1, 1, 0}, {0, 1, 1, 1} };    
4       int mines[4][4] = { {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} };    
5       int w=4, h=4;    
6       for(int y=0; y<h; y++) {   y<4
7           for(int x=0; x<w; x++) {   x<4
8               if(field[y][x]==0) continue;   continue → 처음으로 돌아감(6줄 for문)
9               for(int j=y-1; j<=y+1; j++) {    
10                     for(int i=x-1; i<=x+1; i++) {    
11                               if(chkover(w, h, j, i) == 1)    
12                             mines[j][i] +=1;    
13                      }    
14                 }    
15             }    
16       }    
17 }    
18 int chkover(int w, int h, int j, int i) {    
19       if(i>=0 && i<w && j>=0 && j<h) return 1;    
20       return 0;    
21 }    

 

field[4][4]

0 1 0 1
0 0 0 1
1 1 1 0
0 1 1 1

 

mines[4][4]

1 1 1+1+1 1+1
1+1+1 1+1+1+1 1+1+1+1+1 1+1+1
1+1 1+1+1+1 1+1+1+1+1+1 1+1+1+1+1
1+1 1+1+1+1 1+1+1+1+1 1+1+1+1
1 1 3 2
3 4 5 3
3 5 6 4
3 5 5 3

 

 

filed mines chkover()
y x j i  
0 0      
  1 -1 0 0
      1 0
      2 0
    0 0 1
      1 1
      2 1
    1 0 1
      1 1
      2 1
    2 종료    
  2 continue      
  3 -1 2 0
      3 0
      4 0
      5 0
    0 2 1
      3 1
      4 0
    1 2 1
      3 1
      4 0
    2 종료    
  4 종료      
1 0 continue      
  1 continue      
  2 continue      
  3 0 2 1
      3 1
      4 0
    1 2 1
      3 1
      4 0
    2 2 1
      3 1
      4 0
    3 종료    
  4 종료      
2 0 1 -1 0
      0 1
      1 1
    2 -1 0
      0 1
      1 1
    3 -1 0
      0 1
      1 1
  1 1 0 1
      1 1
      2 1
    2 0 1
      1 1
      2 1
    3 0 1
      1 1
      2 1
  2 1 1 1
      2 1
      3 1
    2 1 1
      2 1
      3 1
    3 1 1
      2 1
      3 1
  3 continue      
3 0 continue      
  1 2 2 1
      3 1
      4 0
    3 2 1
      3 1
      4 0
    4 2 0
      3 0
      4 0
  2 2 1 1
      2 1
      3 1
    3 1 1
      2 1
      3 1
  3 2 2 1
      3 1
    3 2 1
      3 1
4 종료