본문 바로가기

Algorithm/Brute Force

(C++) - 백준(BOJ) 9724 번 : Perfect Cube

반응형

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

 

9724번: Perfect Cube

A perfect cube is an integer whose cube root is also an integer. For example 1, 8, 27, 64, 125, etc. are examples of perfect cubes but 9, 25 and 113 are not. Given two positive integers A and B, your task is to calculate the number of perfect cubes in the

www.acmicpc.net

전수조사 문제였습니다.

📕 풀이방법

📔 입력 및 초기화

test case t, 범위 (a, b), map m, 정답을 출력할 변수 ans를 선언 후 입력받습니다.

📔 풀이과정

for loop를 수행해 20억 보다 작은 정육면체 부피의 한 변을 key, 부피를 value로 해 map 변수 m에 저장합니다. int 범위로 변수들을 선언했기에 3번 곱해 저장한다면 overflow가 나므로 주의합니다.

매 test case마다 입력받은 a,b를 가지고 해당 범위 사이의 m원소를 세주어 ans에 더해줍니다.

📔 정답출력

ans를 출력합니다.


📕 Code

#include <bits/stdc++.h>
using namespace std;

int t, a, b, ans;
map <int, int> m;

int main(){
    for(int i = 1; i*i <= 2000000000/i; i++) m[i] = i*i*i;
    cin >> t;
    for(int i = 1; i <= t; i++){
        ans = 0;
        cin >> a >> b;
        for(auto el : m){
            int pCube = el.second;
            if(a <= pCube && pCube <= b) ans++;
        }
        printf("Case #%d: %d\n", i, ans);
    }
}

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.