1. 문제
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com

2. 어떻게 풀까
처음에 문제를 잘못 읽어서 횟수가 10번인 줄 알고 어떻게 찾아냐 하나 난감했는데, 1번만 연산하면 되는 것이었다.
잘 알려진 공식 `2b = a + c`를 이용하기로 했다.
2b -a -c를 2로 나줘주면 정답이다. 이때 절대값으로 바꿔줘야 한다.하지만 이렇게만 구현하면 오답처리된다.
왜냐하면 수 세개의 부호가 다르면 결과가 다르기 때문이다. 0일 때는 상관 없지만 세 수중 하나라도 부호가 다르면 나누기 2를 하지 않아야 한다. 왜인지는 잘 모르겠다.. 계산해보니 나누기 2를 안하니깐 딱 맞아 떨어진다.
3. 코드
class Solution
{
public static void main(String args[]) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
for(int test_case = 1; test_case <= T; test_case++)
{
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
boolean isPlus = a >=0;
double result = Math.abs(2*b-a-c);
if(isPlus) {
if(b>= 0 && c >= 0) { //
bw.write(String.format("#%d %.1f\n", test_case, result/2));
}
else {
bw.write(String.format("#%d %.1f\n", test_case, result));
}
continue;
}
if(b< 0 && c < 0) { //
bw.write(String.format("#%d %.1f\n", test_case, result/2));
}
else {
bw.write(String.format("#%d %.1f\n", test_case, result));
}
}
bw.flush();
bw.close();
br.close();
}
}'알고리즘 > 백준' 카테고리의 다른 글
| [백준] 케빈 베이컨의 6단계 법칙 : 1389 : Java - BFS (S1) (3) | 2023.11.21 |
|---|---|
| [백준] 리모컨 : 1107 - 완전 탐색 (G5) (1) | 2023.11.20 |
| [백준] 최소 힙 : 1927 - 우선순위 큐 (S2) (0) | 2023.11.17 |
| [S1] Z : 1074 - 분할정복 (0) | 2023.11.17 |
| [S2] 유기농 배추 : 1012 - BFS (0) | 2023.11.17 |