문제 출처
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; } |
'Algorithm' 카테고리의 다른 글
[BOJ 11051] 이항계수2 - 동적계획법 (0) | 2018.11.09 |
---|---|
[BOJ 11050] 이항계수 (0) | 2018.11.09 |
[SW Expert Academy] 1204 최빈수 구하기 (0) | 2018.11.06 |
[자료구조] - 코딩인터뷰 스택과 큐 (0) | 2018.11.02 |
[Algospot] PI 원주율 외우기 (0) | 2018.09.19 |