[C++] 백준 2501 - 약수 구하기

메르센고수·2023년 8월 8일
0

Baekjoon

목록 보기
9/48
post-thumbnail

문제 - 약수 구하기 (Bronze 3)

[백준 2501] https://www.acmicpc.net/problem/2501

풀이 전략

  • 입력 받은 N을 i로 나눴을 때 나머지가 0이 되는 수를 배열에 저장 한 뒤, K번째 (index=K-1) 수를 출력

소스 코드

#include <iostream>
#include <vector>
using namespace std;

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int N,K;
    cin>>N>>K;

    vector<int> A(N,0);

    int j=0;
    for(int i=1;i<=N;i++){
        if(N%i==0){
            A[j]=i;
            j++;
        } // i가 N의 약수이면 A 배열에 i를 저장
    }
    if(A.size()>=K){
        cout<<A[K-1]<<" "; // A배열에 원소가 K보다 더 있다면 K번째 값 출력
    }else{
        cout<<0<<endl;
    }
    return 0;
}

결과

profile
블로그 이전했습니다 (https://phj6724.tistory.com/)

0개의 댓글