-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
15 ljedd2 #174
15 ljedd2 #174
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ λ μ 체μ μΈ κ΅¬ν λ°©μμ λΉμ·νκ² νμ΄λμ΅λλ€! μμλ ꡬν λλ§λ€ λ°©λ²μ΄ κΈ°μ΅ λμ§ μλ κ±° κ°μμ... λ°λ³΅μ μΌλ‘ ꡬνν΄λ³΄κ±°λ μμ μΈμλ²λ¦¬λ ν΄μΌκ² μ΅λλ€ γ γ
μ΄λ² μ°¨μλ μκ³ λ§μΌμ ¨μ΄μ!
import java.io.BufferedReader
import java.io.InputStreamReader
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val sb = StringBuilder()
val T = br.readLine().toInt()
val nums = IntArray(T) { 0 }
for(i: Int in 0..T-1) {
nums[i] = br.readLine().toInt()
}
val primes = mutableListOf<Int>()
for (i in 2..1000) {
var isPrime = true
for (p in primes) {
if (p * p > i) break // μ΄λ―Έ ꡬν μμλ‘λ§ λλ μ νμΈ
if (i % p == 0) {
isPrime = false
break
}
}
if (isPrime) {
primes.add(i) // μμλ©΄ 리μ€νΈμ μΆκ°
}
}
for(i: Int in 0..T-1) {
val result = searchThreeNum(nums[i], primes)
if (result != null) {
sb.append("${result.first} ${result.second} ${result.third}\n")
} else {
sb.append("0\n")
}
}
print(sb)
}
fun searchThreeNum(n: Int, primes: List<Int>): Triple<Int, Int, Int>? {
for (i in primes) {
for (j in primes) {
for (k in primes) {
if (i + j + k == n) {
return Triple(i, j, k)
}
}
}
}
return null
}
# μλΌν μ€ν λ€μ€μ 체 - μμ νλ³ | ||
prime = [True] * 1001 | ||
|
||
for i in range(2,101): | ||
if prime[i]: | ||
for j in range(i*2, 1001, i): | ||
prime[j] = False | ||
|
||
# μμ μ°ΎκΈ° | ||
arr = [i for i in range(2, 1001) if prime[i] == True] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μμλ₯Ό ꡬν λ μ λ μλ μ½λμ²λΌ ꡬννλλ°, μλΌν μ€ν λ€μ€μ 체λ₯Ό μ΄μ©νλ©΄ μ«μκ° λ 컀μ‘μ λλ μκ° λ³΅μ‘λ μΈ‘λ©΄μμ μ 리νκ² κ΅¬νν μ μκ² λ€μ! μ κ° μ¬μ©ν λ°©μλ λ°±μ€μμλ ν΅κ³ΌλκΈ°λ νμ§λ§, μ«μκ° μ»€μ§μλ‘ λλμ μ°μ°μ΄ λμ΄λμ μ’ λΉν¨μ¨μ μΌ κ±° κ°λ€μ π₯Ή
val primes = mutableListOf<Int>()
for (i in 2..1000) {
var isPrime = true
for (p in primes) {
if (p * p > i) break // μ΄λ―Έ ꡬν μμλ‘λ§ λλ μ νμΈ
if (i % p == 0) {
isPrime = false
break
}
}
if (isPrime) {
primes.add(i) // μμλ©΄ 리μ€νΈμ μΆκ°
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ λ μλΌν μ€ν
λ€μ€μ μ²΄λ‘ κ΅¬νν΄λ³΄μμ΅λλ€!
μ½λκ° κ±°μ λμΌνλ€μ γ
γ
μ΄ μκ³ λ¦¬μ¦μ νλ² λ³΄κ³ λ€λλ©΄ κΉλ¨Ήκ΅¬,,,μ μκ² κ·Έλ° μκ³ λ¦¬μ¦μ΄λ€μ γ
μ λ―Έλ λ§μλλ‘ μΈμλ²λ¦¬λ μ§ ν΄μΌκ² μλλ€ππ
def find_prime_sum(K, primes):
for x in primes:
for y in primes:
for z in primes:
if x + y + z == K:
print(x, y, z)
return
print(0)
# μλΌν μ€ν
λ€μ€μ μ²΄λ‘ μμ ꡬνκΈ°
is_prime = [True] * 1001
for i in range(2, int(1001 ** 0.5) + 1):
if is_prime[i]:
for j in range(i * i, 1001, i):
is_prime[j] = False
primes = [i for i in range(2, 1001) if is_prime[i]]
for _ in range(int(input())):
K = int(input())
find_prime_sum(K, primes)
λ€μ PRλ νν μ λλ€!
π λ¬Έμ λ§ν¬
λ°±μ€ λΈλ£¨νΈν¬μ€ | BOJ 11502 μΈ κ°μ μμ λ¬Έμ
λ¬Έμ μ€λͺ
μ μλ‘ (μν)μμ, μΈ κ°μ μμ λ¬Έμ (3-primes problem) λ λ€μκ³Ό κ°μ μΆμΈ‘μ λ§νλ€.
'5λ³΄λ€ ν° μμμ νμλ μ νν μΈ κ°μ μμλ€μ ν©μΌλ‘ λνλΌ μ μλ€. λ¬Όλ‘ νλμ μμλ₯Ό μ¬λ¬ λ² λν μλ μλ€.'
μλ₯Ό λ€λ©΄,
7 = 2 + 2 + 3
11 = 2 + 2 + 7
25 = 7 + 7 + 11
5λ³΄λ€ ν° μμμ νμλ₯Ό μ λ ₯λ°μμ, κ·Έ νμκ° μ΄λ»κ² μΈ μμμ ν©μΌλ‘ ννλ μ μλμ§ μμ보μ.
βοΈ μμλ μκ°
1μκ°
β¨ μλ μ½λ
μλΌν μ€ν λ€μ€ 곡μμ κΉλ¨Ήμ΄μ .. ππ ν λλ μ’ μ€λ κ±Έλ¦° κ² κ°μλ°
μ 리νλ μμ² κ°λ¨νκ² ν μ μλ λΉκ΅μ μ¬μ΄ λ¬Έμ λ€μ
μ΄ λ€μμ°¨μμλ μ’ λ μ΄λ €μ΄ λ¬Έμ λ₯Ό λ€κ³ μ€κ² μ΅λλ€ .. !
μ΄ λ¬Έμ λ₯Ό νκΈ° μν΄μλ μμλ₯Ό ꡬνλ μλΌν μ€ν λ€μ€μ 체λ₯Ό μμμΌ ν©λλ€!
μ 체 μκ° λ³΅μ‘λλ O(nΒ³)λ‘
nμ΄ 1000μ΄ μλλΌ 1000κΉμ§μ μμμ κ°μλΌ λλ΅ 168κ°λΌκ³ μΉλ©΄
μ€μ μ°μ° νμλ μ½ 168Β³ = 4,741,632ν μ λκ° λμ΄ λΈλ£¨νΈν¬μ€λ‘ μΆ©λΆν κ³μ°ν μ μμ΅λλ€.
μλΌν μ€ν λ€μ€μ 체 μκ³ λ¦¬μ¦μ μ¬μ©νμ¬ μ£Όμ΄μ§ Kλ³΄λ€ μμ μμλ€μ ꡬν ν
3μ€ forλ¬ΈμΌλ‘ λͺ¨λ κ²½μ°λ₯Ό νμν΄λ³΄λ λΈλ£¨νΈ ν¬μ€(Brute Force) μκ³ λ¦¬μ¦ κΈ°λ²μ μ¬μ©νμ¬ Kλ₯Ό λ§λ€ μ μλ μΈ κ°μ μμλ₯Ό μ°Ύμλ΄λ©΄ λ©λλ€.
π μλ‘κ² μκ²λ λ΄μ©