티스토리 뷰

 

 

 

구름에듀 4차 9번 문제

 

 

 

 

 

 

 


 

 

 

 

 

 

 

주어진 코드

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int height[][4], int height_len) {
	int count = 0;
    
    
							//빈칸
                            
                            
	return count;
}
int main() {
	int height[4][4] = { {3, 6, 2, 8}, {7, 3, 4, 2}, {8, 6, 7, 3}, {5, 3, 2, 9} };
	int height_len = 4;
	int ret = solution(height, height_len = 4);

	printf("solution 함수의 반환 값은 %d 입니다.\n", ret);
}

 

 


 

 

완성 코드

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int height[][4], int height_len) {
	int count = 0;
	for (int i = 0; i < height_len; i++) {
		for (int j = 0; j < height_len; j++) {
			if (height[i][j] < height[i][j - 1] && height[i][j] < height[i + 1][j] && height[i][j] < height[i][j + 1] && height[i][j] < height[i - 1][j]) {
				count++;
			}
		}
	}
	return count;
}
int main() {
	int height[4][4] = { {3, 6, 2, 8}, {7, 3, 4, 2}, {8, 6, 7, 3}, {5, 3, 2, 9} };
	int height_len = 4;
	int ret = solution(height, height_len = 4);

	printf("solution 함수의 반환 값은 %d 입니다.\n", ret);
}

 

예시

height height_len return
[[3, 6, 2, 8], [7, 3, 4, 2], [8, 6, 7, 3], [5, 3, 2, 9]] 4 5

 

 

 

 

 

 

 

부가 설명

각 위치별로 위험지역인지 판별하기 위해서는 
위치의 위, 왼쪽, 아래, 오른쪽의 숫자가 위치의 값보다 큰지 확인해야한다.

따라서 &&연산자를 사용하여
위 왼 아 오의 값이 모두 클 때 count++를 이용하여
위험지역의 갯수를 셀 수 있다.

 

 

 

 

 

 

 


 

 

 

 

 

GroomEdu

 

goorm

구름은 클라우드 기술을 이용하여 누구나 코딩을 배우고, 실력을 평가하고, 소프트웨어를 개발할 수 있는 클라우드 소프트웨어 생태계입니다.

www.goorm.io

 

댓글