티스토리 뷰

 

 

 

구름에듀 6차 1번 문제

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

주어진 코드

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int temperature[], int temperature_len, int A, int B) {
	int answer = 0;
	
    	//빈칸
    
	return answer;
}
int main() {
	int temperature[] = { 3, 2, 1, 5, 4, 3, 3, 2 };
	int temperature_len = 8;
	int A = 1;
	int B = 6;
	int ret = solution(temperature, temperature_len, A, B);

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

 

 

 

 

완성 코드

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int temperature[], int temperature_len, int A, int B) {
	int answer = 0;
	for (int i = A; i < B; i++) {
		if (temperature[i] > temperature[A] && temperature[i] > temperature[B]) {
			answer++;
		}
	}
	return answer;
}
int main() {
	int temperature[] = { 3, 2, 1, 5, 4, 3, 3, 2 };
	int temperature_len = 8;
	int A = 1;
	int B = 6;
	int ret = solution(temperature, temperature_len, A, B);

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

 

 

예시

temperature temperature_len A B return
[3,2,1,5,4,3,3,2] 8 1 6 2

 

 

 

 

 

 

 

 

 

코드 분석

단계 과정
main 각 날짜별 평균 기온이 담긴 배열 temperature과 그 길이를 선언한다.
첫 날짜와 마지막 날짜를 설정한 변수 A와 B에 값을 지정한다.
solution함수를 호출한다.
solution 평균기온보다 높았을 때 갯수를 셀 변수 answer를 선언한다.
solution - for A일부터 B일까지, for문을 반복한다.
&&연산자를 사용하여 temperature방의 값이 A일의 기온과 B일의 기온보다 모두 높았을 때
answer++를 진행한다.
solution 최종 갯수 answer를 반환한다.
main 결과를 출력하고 프로그램을 종료한다.

 

 

 

 

 

 

 


GroomEdu

 

goorm

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

www.goorm.io

 

 

 

 

댓글