본문 바로가기

CodingTest Exam/[C++] Algorithm Study

24. Jolly Jumpers (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★★☆☆☆

#include<stdio.h>
#include<vector>
#include<cmath>
#include<algorithm>

using namespace std;
int main()
{
	int n;
	scanf("%d", &n);
	
	vector<int> num(n);
	for(int i = 0; i < n; i++)
		scanf("%d", &num[i]);
	
	bool flag = true;
	vector<int> answer;
	for(int i = 0; i < n-1; i++)
	{
		int gap = abs(num[i] - num[i+1]);
		answer.push_back(gap);
	}
	
	for(int i = 1; i < n; i++)
		if(!count(answer.begin(), answer.end(), i))
			flag = false;
	
	if(flag)
		printf("YES");
	else
		printf("NO");
	return 0;
}

1. <cmath> abs(x) : x의 값을 절댓값으로 변환(두 수의 차이를 계산할 때) 

2. <algorithm> count(iterator a, iterator b, Key) : a부터 b까지 Key의 포함여부를 확인해서 boolean으로 return

 

첫 점수 : 80점

- 1부터 시작인 것을 0부터 시작해서 count여부 확인