π₯ PS(Problem Solving) π₯/goorm
[ꡬλ¦LEVEL] λ§κ°μ§ μλΌν μ€ν λ€μ€μ 체
dar0m!
2020. 3. 27. 03:16
λμ΄λ | μ λ΅λ₯ |
β β | -% |
ν리미μ μκ³ λ¦¬μ¦ μν΄λ¦¬ λΉνμκ³ μμ¦3 4μ£Όμ°¨
goorm
ꡬλ¦μ ν΄λΌμ°λ κΈ°μ μ μ΄μ©νμ¬ λꡬλ μ½λ©μ λ°°μ°κ³ , μ€λ ₯μ νκ°νκ³ , μννΈμ¨μ΄λ₯Ό κ°λ°ν μ μλ ν΄λΌμ°λ μννΈμ¨μ΄ μνκ³μ λλ€.
www.goorm.io

λ¬Έμ

ν΄κ²°
- 1μ μμκ° μλλ μ μΈνκ³ 2λΆν° μμ.
- iλ‘ λλμ΄μ§λ nμ λνμ¬
- iκ° μμμ΄κ³ , (n / i)λ μμλΌλ©΄ κ±Έλ¬μ§λ€ β 1
- iλ (n / i)μ€ νλλΌλ μμκ° μλλΌλ©΄ ν΄λΉ nμ λ§κ°μ§ μλΌν μ€ν λ€μ€μ 체μ κ±Έλ¬μ§μ§ μλλ€. β 0
- i*iκ° nμΌ λκΉμ§ λ°λ³΅λ¬Έμ λλ©΄μ ν λ²μ΄λΌλ λλμ΄μ§λ iλ₯Ό λ°κ²¬νμ§ λͺ»νλ€λ©΄ κ·Έ μλ κ·Έ μμ²΄λ‘ μμμ΄λ€. β 1
μ½λ
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
29
30
31
32
33
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
ll n;
bool isPrime(ll num) {
for (ll i = 2; i * i <= num; i++) {
if (!(num % i)) return 0;
}
return 1;
}
int main() {
scanf("%lld", &n);
if (n == 1) {
printf("0");
return 0;
}
for (ll i = 2; i * i <= n; i++) {
if (!(n % i)) {
if (isPrime(i) && isPrime(n / i)) {
printf("1");
return 0;
}
else { // νλλΌλ λλ μ§λλ° μμλ€μ κ³±μ΄ μλλ©΄ 0
printf("0");
return 0;
}
}
}
printf("1");
return 0;
}
|
cs |
1λΆν° μμνλ κ²½μ°
1λΆν° νμΈνλ κ²½μ°μλ μμνλ³νλ ν¨μμΈ isPrimeμμ 1μΌ λ 0μ λ°ννλ λ¬Έμ₯μ μΆκ°ν΄μΌ νλ€.
bool isPrime(ll num) {
if (num == 1) return 0;
for (ll i = 2; i * i <= num; i++) {
if (!(num % i)) return 0;
}
return 1;
}