• 문제 링크
25707번: 팔찌 만들기
N개의 구슬을 모두 사용하여 조건에 맞게 팔찌를 만들 때 사용하는 줄의 길이의 최솟값을 출력한다.
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read();
int[] arr = new int[n];
while (n-- > 0) arr[n] = read();
Arrays.sort(arr);
int sum = Math.abs(arr[0] - arr[arr.length - 1]);
for (int i = 1; i < arr.length; i++) sum += Math.abs(arr[i - 1] - arr[i]);
bw.write(String.valueOf(sum));
bw.flush();
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read();
int min = Integer.MAX_VALUE, max = 0;
while (n-- > 0) {
int i = read();
min = Math.min(min, i);
max = Math.max(max, i);
}
bw.write(String.valueOf((max - min) * 2));
bw.flush();
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 28464 Potato - Greedy / Java (0) | 2023.09.24 |
---|---|
[백준] 28353 고양이 카페 - Greedy / Java (0) | 2023.09.23 |
[백준] 29615 알파빌과 베타빌 - Greedy / Java (0) | 2023.09.21 |
[백준] 28324 스케이트 연습 - Greedy / Java (0) | 2023.09.20 |
[백준] 23351 물 주기 - Greedy / Java (0) | 2023.09.19 |
댓글