Algorithm/BOJ
백준 2960번 C++
poopooreum
2023. 8. 15. 08:08
반응형
백준 2960번 에라토스테네스의 체
https://www.acmicpc.net/problem/2960
2960번: 에라토스테네스의 체
2, 4, 6, 8, 10, 3, 9, 5, 7 순서대로 지워진다. 7번째 지워진 수는 9이다.
www.acmicpc.net



정답 코드
#include <iostream>
#include<vector>
using namespace std;
vector<bool> numArr;
int n, cnt=0,k;
int solve(int n, int k) {
int min = 2;
numArr.resize(n + 1, 1); //n을 하나크게 만들어서 0
for (int i = 2; i <=n; i++) {
for(int j = i;j<=n;j+=i){
if (numArr[j]) {
numArr[j] = 0;
cnt++;
if (cnt == k)
return j;
}
}
}
return 0;
}
int main()
{
cin >> n >> k;
cout << solve(n, k);
return 0;
}
반응형