Skip to content
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

Merged
merged 15 commits into from
Nov 22, 2024
Merged

15 ljedd2 #174

merged 15 commits into from
Nov 22, 2024

Conversation

LJEDD2
Copy link
Collaborator

@LJEDD2 LJEDD2 commented Oct 8, 2024

πŸ”— 문제 링크

λ°±μ€€ 브루트포슀 | 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λ₯Ό λ§Œλ“€ 수 μžˆλŠ” μ„Έ 개의 μ†Œμˆ˜λ₯Ό μ°Ύμ•„λ‚΄λ©΄ λ©λ‹ˆλ‹€.

# ν•˜λ‚˜μ˜ μ†Œμˆ˜λ₯Ό μ—¬λŸ¬ 번 더할 수 μžˆλ‹€. -> 브루트포슀 κ°€λŠ₯ 
# 5보닀 큰 μž„μ˜μ˜ ν™€μˆ˜λ‘œ μ„Έ μ†Œμˆ˜μ˜ 합이 λ˜λŠ”μ§€ 
def search(n):
    for i in arr:
        for j in arr:
            for k in arr:
                if i+j+k == n:
                    return i, j, k

    else: # μ•ˆλ˜λ©΄ 0 
        return 0

# μ—λΌν† μŠ€ν…Œλ„€μŠ€μ˜ 체 - μ†Œμˆ˜ νŒλ³„ 
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]

t = int(input())
for _ in range(t):
    n = int(input())
    print(*search(n))

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

Copy link
Member

@jung0115 jung0115 left a 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
}

Comment on lines +16 to +25
# μ—λΌν† μŠ€ν…Œλ„€μŠ€μ˜ 체 - μ†Œμˆ˜ νŒλ³„
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]
Copy link
Member

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)  // μ†Œμˆ˜λ©΄ λ¦¬μŠ€νŠΈμ— μΆ”κ°€
  }
}

Copy link
Member

@janghw0126 janghw0126 left a 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도 νŒŸνŒ…μž…λ‹ˆλ‹€!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants