6742번 : Next in line

상환·2021년 12월 6일
0
post-thumbnail

문제

You know a family with three children. Their ages form an arithmetic sequence: the difference in ages between the middle child and youngest child is the same as the difference in ages between the oldest child and the middle child. For example, their ages could be 5, 10 and 15, since both adjacent pairs have a difference of 5 years.

Given the ages of the youngest and middle children, what is the age of the oldest child?

입출력

1. 입력

The input consists of two integers, each on a separate line. The first line is the age Y of the youngest child (0 ≤ Y ≤ 50). The second line is the age M of the middle child (Y ≤ M ≤ 50).

2. 출력

The output will be the age of the oldest child.

풀이

문제를 해석하면

애 셋이 있다. 이 셋의 나이차이는 등차수열이다. 젤 작은 놈이랑 중간 놈 나이차이가 중간 놈 큰 놈 나이차이랑 같다. 젤 작은 놈이랑 중간 놈 나이를 주면 큰 놈 나이는 몇 살일까?

로 해석할 수 있다.
젤 작은 놈은 (0 ≤ Y ≤ 50)이고 중간 놈은 (Y ≤ M ≤ 50)으로 주므로 큰 놈 = 중간 놈 + 나이차이 인것을 알 수 있다.

import java.io.*;

public class Main {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        try {
            int min=Integer.parseInt(br.readLine());
            int middle=Integer.parseInt(br.readLine());
            br.close();
            bw.write(Integer.toString(middle+(middle-min)));
            bw.flush();
            bw.close();
        } catch (IOException e){
            e.printStackTrace();
        }
    }
}

결과는

풀었다.

kotlin code

import java.io.*
import java.nio.Buffer

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val bw = BufferedWriter(OutputStreamWriter(System.out))

    try {
        val min = br.readLine().toInt()
        val middle = br.readLine().toInt()
        br.close()
        bw.write((middle+(middle-min)).toString())
        bw.flush()
        bw.close()
    }catch (e: IOException){
        e.stackTrace
    }
}
profile
레모네이드 커피

0개의 댓글