문제 분석

처음지점 - 마지막 지점 의 값과

바로가기 버튼 - 마지막 지점 의 값을 비교만 하면된다.

절대값으로 비교해야 하기에 Math.abs를 이용했다.

 

 

 

 

전체 소스

package test;

import java.util.*;
import java.io.*;

public class Main {

	public static void main(String[] args) throws Exception {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine()," ");
		int start = Integer.parseInt(st.nextToken());
		int end = Integer.parseInt(st.nextToken());
		int repeat = Integer.parseInt(br.readLine());
		int max = Math.abs(start - end);
		int result = max;

		for(int i=0; i<repeat; i++) {
			int target = Integer.parseInt(br.readLine());
			
			if(max>Math.abs(end-target)) {
				max=Math.abs(end-target);
				result = max+1;
			}
		
		}
		System.out.println(result);
		

		

	}

}

 

 

 

 

 


https://www.acmicpc.net/problem/3135

 

3135번: 라디오

첫 줄엔 정수 A와 B가 주어진다 (1 ≤ A, B < 1000, A ≠ B). 다음 줄엔 정수 N이 주어진다 (1 ≤ N ≤ 5). 다음 N개의 줄엔 미리 지정되어 있는 주파수가 주어진다 (주파수는 1000 보다 작다).

www.acmicpc.net

 

'알고리즘(BOJ) > Silver' 카테고리의 다른 글

백준 - 30 10610  (0) 2023.02.27
백준 - 주유소 13305  (0) 2023.02.23
백준 - 거스름돈 14916  (0) 2023.02.13
백준 - ATM 11399  (0) 2023.02.11
백준 - 섬의 개수 4963  (0) 2023.02.10
복사했습니다!