Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cpp] 백준 2014 소수의 곱 - Gold I (민코딩 Ugly Number) #51

Merged
merged 3 commits into from
Aug 15, 2023

Conversation

minjae9610
Copy link
Member

@minjae9610 minjae9610 commented Aug 5, 2023

🌁 Background

민코딩 과제

👩‍💻 Contents

BoJ 2014 소수의 곱

image

#include <iostream>
#include <limits>

using namespace std;

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


    int k, n;
    cin >> k >> n;

    pair<int, int> prime_numbers[100];
    for (int i = 0; i < k; ++i) {
        cin >> prime_numbers[i].first;
        prime_numbers[i].second = 0;
    }
    int ugly_numbers[100001] = { 1 };

    for (int i = 1; i < n + 1; ++i)
    {
        int min_number = numeric_limits<int>::max();
        int min_index[100];
        int min_count = 0;
        for (int j = 0; j < k; ++j)
        {
            int next_number = prime_numbers[j].first * ugly_numbers[prime_numbers[j].second];
            if (next_number < min_number)
            {
                min_number = prime_numbers[j].first * ugly_numbers[prime_numbers[j].second];
                min_index[0] = j;
                min_count = 1;
            }
            else if (next_number == min_number)
            {
                min_index[min_count] = j;
                ++min_count;
            }
        }
        ugly_numbers[i] = min_number;
        for (int j = 0; j < min_count; ++j)
            ++prime_numbers[min_index[j]].second;
    }

    cout << ugly_numbers[n] << endl;

    return 0;
}

📱 Screenshot

image
image

📝 Review Note

우선순위 큐 카테고리의 문제지만 투포인터를 응용해 각 소수 별 포인터를 관리해서 풀면 훨씬 빠른 시간 내로 연산을 마칠 수 있다.

📬 Reference

@minjae9610 minjae9610 added 알고리즘 알고리즘 문제 백준 백준 문제 민코딩 민코딩 문제 #PriorityQueue #수학 labels Aug 5, 2023
@minjae9610 minjae9610 requested a review from a team as a code owner August 5, 2023 14:41
@minjae9610 minjae9610 requested review from KIMSEI1124 and bellday and removed request for a team August 5, 2023 14:41
Copy link
Contributor

@KIMSEI1124 KIMSEI1124 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C++ 문법중 궁금한 부분 질문입니다!

@minjae9610 minjae9610 merged commit dfbcd41 into main Aug 15, 2023
@minjae9610 minjae9610 deleted the algo/minjae9610/boj2014 branch August 15, 2023 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
#PriorityQueue #수학 민코딩 민코딩 문제 백준 백준 문제 알고리즘 알고리즘 문제
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants