[백준]2960_에라토스테네스의 채

🐈 JAELEE 🐈·2021년 10월 7일
0

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

Solved

✔ 소수찾기? 에라토스테네스의 채
✔ (복습) 최대공약수찾기? 유클리드 호제법

//에라토스테네스의 채
using namespace std;
#include <iostream>
#define MAX 1001

int e[MAX]; //0: 소수, 1:소수가 아님

void era() {
	e[1]=1;
    for(int i=2;i<MAX; i++) {
    	for(int j=i*2; j<MAX; j+=i) {
        	if(e[j]==1) continue;
            else e[j]=1;
        }
    }
}
using namespace std;
#include <iostream>
#define MAX 1001

int n, k;
int e[MAX];
int Eratos() {
	int cnt = 0;
	e[1] = 1;
	for (int i = 2; i <= n; i++) {
		if(e[i]==0) cnt++;
		if (cnt == k) return i;
		for (int j = i * 2; j <= n; j += i) {
			if (e[j] == 1) continue;
			else {
				e[j] = 1;
				cnt++;
				if (cnt == k) return j;	
			}
		}
	}
}

int main() {	
	cin >> n >> k;
	cout << Eratos() << '\n';
}

0개의 댓글