Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 28 |
Tags
- notification
- Navigation
- CustomView
- ViewModel
- Coroutine
- recyclerview
- 백준
- Behavior
- 알림
- onLayout
- CoordinatorLayout
- CollapsingToolbarLayout
- LiveData
- View
- lifecycle
- AppBarLayout
- 코틀린
- Android
- kotlin
- HTTP
- sqlite
- hilt
- Algorithm
- 안드로이드
- BOJ
- activity
- onMeasure
- DataBinding
- 알고리즘
- room
Archives
- Today
- Total
개발일지
Algorithm in A..Z - 에라토스테네스의 체 본문
개념
소수를 판별하는 알고리즘
작동원리
1. 2부터 시작한다.
2. 방문하지 않은 숫자이면 그 숫자의 배수를 전부 체크한다.
3. 체크되지 않은 숫자는 소수이다.
시간복잠도
O(NloglogN)
문제
1978 소수찾기
1978번: 소수 찾기
첫 줄에 수의 개수 N이 주어진다. N은 100이하이다. 다음으로 N개의 수가 주어지는데 수는 1,000 이하의 자연수이다.
www.acmicpc.net
코드
#include <iostream>
#include <vector>
using namespace std;
constexpr int MAX = 1000;
int main() {
vector<bool> isPrime(MAX, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i*i <= MAX; ++i) {
if (isPrime[i]) {
for (int j = i*i; j <= MAX; j += i) {
isPrime[j] = false;
}
}
}
int n;
cin >> n;
int answer = 0;
while (n--) {
int num;
cin >> num;
answer += isPrime[num];
}
cout << answer << "\n";
}
'Algorithm (알고리즘)' 카테고리의 다른 글
Algorithm in A..Z - Dijkstra (0) | 2020.12.01 |
---|---|
Algorithm in A..Z - 유클리드 호제법 (0) | 2020.12.01 |
Android in A..Z - DFS (0) | 2020.11.28 |
Algorithm in A..Z - BFS (0) | 2020.11.28 |
Algorithm in A..Z - Binary Matching (Hopcroft-Karp) (0) | 2020.10.16 |
Comments