백준 c++ 3135 라디오

jaranda·2022년 6월 25일

3135 라디오



문제풀이

#include <iostream>
#include <stdlib.h>
using namespace std;
void fast_io(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
int from,to,n,gap,idx=100;
int arr[7];
int main(void)
{
	fast_io();
	cin >> from >> to >> n;
	for (int i = 0;i<n;i++)
		cin >> arr[i];
	gap = abs(to - from);
	for (int i = 0; i < n;i++)
	{
		int tmp = abs(arr[i] - to);
		if (gap > tmp)
		{
			gap = tmp;
			idx = i;
		}
	}
	if(idx!=100)
		cout << abs(arr[idx] - to) + 1;
	else
		cout << abs(to - from);
}

절대값차이를 구해서 더 빠른 방법으로 채널을 바꿀 수 있게 했다.

profile
자라는 개발자

0개의 댓글