Post

[백준] 16479번 - 컵라면 측정하기 [Java][C++]

[백준] 16479번 - 컵라면 측정하기 [Java][C++]

문제 링크


1. 문제 풀이

원뿔대의 윗면의 지름$(D_1)$, 아랫면의 지름$(D_2)$, $K$ 가 주어졌을 때, 컵라면의 높이의 제곱$(H^2)$을 구해야 하는 문제로 피타고라스 정리를 활용하면 해결할 수 있다.


2. 코드

1. 피타고라스 정리 [Java]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        int K = Integer.parseInt(br.readLine());

        st = new StringTokenizer(br.readLine());
        int D1 = Integer.parseInt(st.nextToken());
        int D2 = Integer.parseInt(st.nextToken());

        System.out.println(K * K - Math.pow((D1 - D2), 2) / 4);
    }
}

2. 피타고라스 정리 [C++]

1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int k, d1, d2;
    cin >> k >> d1 >> d2;
    cout << k * k - pow(d1 - d2, 2) / 4;
}

3. 풀이 정보

1. 피타고라스 정리 [Java]

언어시간메모리코드 길이
Java 11104 ms14252 KB508 B

2. 피타고라스 정리 [C++]

언어시간메모리코드 길이
C++ 170 ms2020 KB204 B

This post is licensed under CC BY 4.0 by the author.