본문 바로가기

Algorithm/Implementation

(C++) - 백준(BOJ) 5163 : Isn’t It Funny How a Bear Likes Honey?

반응형

https://www.acmicpc.net/problem/5163

 

5163번: Isn’t It Funny How a Bear Likes Honey?

For each data set, output “Data Set x:” on a line by itself, where x is its number. On the next line, output “Yes” or “No”, depending on whether the balloons together will be able to lift Pooh. Each data set should be followed by a blank line.

www.acmicpc.net

지문 그대로 구현하는 문제였습니다.

📕 풀이방법

📔 입력 및 초기화

1. k를 선언 후 입력해줍니다. 2. k만큼 for loop를 수행하며 풍선개수 balloonNum, 푸의 무게 weight를 선언 후 입력받습니다. 3. 외에 각 풍선마다 입력받을 radius, 정답을 출력하기 위한 변수 totalHelium을 선언해줍니다. 4. 이후 각 풍선의 반지름을 입력해줍니다. 

📔 풀이과정

 1. 1g당 1000cm^3의 헬륨이 필요합니다. 각 풍선의 반지름이 입력될 때마다 구의 공식 4/3*pi*r^3 으로 헬륨의 부피를 구한 후 이를 gram으로 환산해줍니다. 이 값을 totalHelium에 더해줍니다.

 2. 구한 값은 helium이 들어있는 풍선들의 버틸 수 있는 최종 몸무게가 되며 이를 weight와 비교해 답을 구합니다.

📔 정답출력

형식에 맞춰 출력해줍니다.


📕 Code

#include <bits/stdc++.h>
#define pi 3.14159265359
using namespace std;
int k;

int main(){
    cin >> k;
    for(int i = 1; i <= k; i++){
        double balloonNum, weight, radius, totalHelium = 0;
        cin >> balloonNum >> weight;
        for(int j = 1; j <= balloonNum; j++){
            cin >> radius;
            totalHelium += 4.0 / 3.0 * pi * pow(radius, 3) / 1000;
        }
        printf("Data Set %d:\n", i);
        if(totalHelium >= weight) cout << "Yes";
        else cout << "No";
        printf("\n\n");
    }
}