반응형


문제 출처

https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV134DPqAA8CFAYh


직관적인 방법으로 풀이하였습니다. 처음에 ret 변수를 전역변수로 두고 매 실행시 초기화를 하지 않아 테스트케이스 오류가 나와 많이 헤맸던 문제.... 역시 처음부터 실수하지 않고 짜는게 중요한거 같습니다. 제가 짠 코드에서 오류를 찾으려니 너무 어렵군요 ㅠㅠ 문제자체는 간단한 문제입니다!


#include <iostream>

#include <algorithm>

#include <string.h>


using namespace std;


const int MAX = 1000;

int map[MAX];

int caseNum;

void solution()

{

    int tc;

    int ret=0, tmp=0;

    memset(map, 0, sizeof(map));

    caseNum++;

    scanf("%d", &tc);

    for(int i=0;i<tc;i++)

       scanf("%d", &map[i]);

    

    for(int i=2;i<tc-2;i++)

    {

        tmp = map[i] - max(max(map[i-1], map[i-2]), max(map[i+1], map[i+2])); 

        if(tmp > 0)

            ret+=tmp;

    }

    cout<<"#"<<caseNum<<" "<<ret<<endl;

}

int main(void)

{

    for(int i=0;i<10;i++)

        solution();

    return 0;   





반응형

+ Recent posts