forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2.cpp
More file actions
27 lines (24 loc) ยท 822 Bytes
/
2.cpp
File metadata and controls
27 lines (24 loc) ยท 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <bits/stdc++.h>
using namespace std;
int n = 1000; // 2๋ถํฐ 1,000๊น์ง์ ๋ชจ๋ ์์ ๋ํ์ฌ ์์ ํ๋ณ
// ์ฒ์์ ๋ชจ๋ ์๊ฐ ์์(True)์ธ ๊ฒ์ผ๋ก ์ด๊ธฐํ(0๊ณผ 1์ ์ ์ธ)
vector<int> arr(n + 1, true);
int main() {
// ์๋ผํ ์คํ
๋ค์ค์ ์ฒด ์๊ณ ๋ฆฌ์ฆ ์ํ
// 2๋ถํฐ n์ ์ ๊ณฑ๊ทผ๊น์ง์ ๋ชจ๋ ์๋ฅผ ํ์ธํ๋ฉฐ
for (int i = 2; i <= (int) sqrt(n); i++) {
// i๊ฐ ์์์ธ ๊ฒฝ์ฐ(๋จ์ ์์ธ ๊ฒฝ์ฐ)
if (arr[i] == true) {
// i๋ฅผ ์ ์ธํ i์ ๋ชจ๋ ๋ฐฐ์๋ฅผ ์ง์ฐ๊ธฐ
int j = 2;
while (i * j <= n) {
arr[i * j] = false;
j += 1;
}
}
}
// ๋ชจ๋ ์์ ์ถ๋ ฅ
for (int i = 2; i <= n; i++) {
if (arr[i]) cout << i << ' ';
}
}