본문 바로가기
Problem Solving/Baekjoon

[백준] 15001 Frog Leaps - Greedy / Java

by graycode 2024. 4. 12.

 문제 링크

 

15001번: Frog Leaps

It is a little known secret that frogs also like to participate in the BAPC. However, to reach Amsterdam they will need to cross lots of rivers. Fortunately, the frog is in good condition and can jump as far as he wants to go. However, jumping a distance o

www.acmicpc.net

 

 풀이 코드

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));

        long n = read(), p = read(), x, sum = 0;
        for (; n > 1; n--, p = x) sum += (long) Math.pow((x = read()) - p, 2);

        bw.write(String.valueOf(sum));
        bw.flush();
    }

    private static long read() throws IOException {
        long c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글