Algorithm
λ¬Έμ μ λͺ©μ ν΄λ¦νλ©΄, GitHub μ½λλ‘, βοΈλ₯Ό ν΄λ¦νλ©΄ ν°μ€ν λ¦¬λ‘ μ΄λν©λλ€.
μ½λ©ν
μ€νΈ μ°μ΅ (λ°±μ€, νλ‘κ·Έλλ¨Έμ€ λ±) μκ³ λ¦¬μ¦
μλ£κ΅¬μ‘°
μκ³ λ¦¬μ¦
μν
μμ΄
μμκ° μλ μμμ μ§ν©, nκ° μ€ rκ°λ₯Ό λ½λ λ°©λ²
Copy var v = [ 1 , 2 , 3 ]
func permutation ( n : Int , r : Int , depth : Int ) {
if r == depth {
print ( v.map { String ( $0 ) }.joined ( separator : " " ))
return
}
for i in depth ..< n {
v. swapAt ( i, depth )
permutation ( n : n, r : r, depth : depth + 1 )
v. swapAt ( i, depth )
}
return
}
permutation ( n : 3 , r : 3 , depth : 0 )
μ‘°ν©
μμκ° μλ μμμ μ§ν©, nκ° μ€ rκ°λ₯Ό λ½λ λ°©λ²
Copy var v = [ 1 , 2 , 3 ]
func permutation ( n : Int , r : Int , depth : Int ) {
if r == depth {
print ( v.map { String ( $0 ) }.joined ( separator : " " ))
return
}
for i in depth ..< n {
v. swapAt ( i, depth )
permutation ( n : n, r : r, depth : depth + 1 )
v. swapAt ( i, depth )
}
return
}
permutation ( n : 3 , r : 3 , depth : 0 )
Copy let n = 5 , r = 3 , a = [ 1 , 2 , 3 , 4 , 5 ]
// r=3μ΄λ―λ‘ 3μ€ forλ¬Έ -> rμ΄ μμ λμλ§ λ°λ³΅λ¬Έμ μ¬μ©νμ
for i in 0 ..< n {
for j in i + 1 ..< i {
for k in j + 1 ..< j {
print ( " \(i) , \(j) , \(k)
}
}
}
// μμλ§ λ€λ₯Ό λΏ
for i in 0..<n {
for j in 0..<i {
for k in 0..<j {
print(" \(i), \(j), \(k)
}
}
}
μ΅λ곡μ½μ
μ ν΄λ¦¬λ νΈμ λ² μ΄μ©
Copy func gcd ( _ a : Int , _ b : Int ) -> Int {
if b == 0 { return a }
return gcd ( b, a % b )
}
μ΅μ곡배μ
Copy func gcd ( _ a : Int , _ b : Int ) -> Int {
if b == 0 { return a }
return gcd ( b, a % b )
}
func lcm ( _ a : Int , _ b : Int ) -> Int {
return a * b / gcd ( a, b )
}
μμ μ°ΎκΈ°
μλΌν μ€ν
λ€μ€μ 체(n <= 1,000,000)
Copy func primeNumbers ( _ n : Int ) -> [ Int ] {
var numbers = [ Bool ] ( repeating : true , count : n + 1 )
var result = [ Int ] ()
for i in 2 ... n {
if numbers [ i ] {
for j in stride ( from : i * 2 , to : n, by : i ) {
numbers [ j ] = false
}
}
}
for i in 2 ... n {
if numbers [ i ] { result. append ( i ) }
}
return result
}
Copy func primeNumbers ( _ n : Int ) -> Set < Int > {
var numbers = Set ( 2 ... n )
for i in 2 ... n {
if numbers. contains ( i ) {
numbers. subtract ( Set ( stride ( from : i * 2 , to : n, by : i )))
}
}
return numbers
}
Copy func isPrimeNumber ( _ n : Int ) -> Bool {
if n < 2 { return false }
if n == 2 { return true }
if n % 2 == 0 { return false }
for i in 2 ... n {
**if ! (i * i <= n) { break } **
if n % i == 0 { return false }
}
return true
}
Copy **import Foundation **
func isPrimeNumber ( _ n : Int ) -> Bool {
if n < 4 { return n >= 2 }
if n % 2 == 0 { return false }
for i in 2 ...** Int ( sqrt ( Double ( n ))) ** {
if n % i == 0 { return false }
}
return true
}
λμ ν©
Copy var cards = [ 0 ]
var prefix = [ 0 ]
let input = readLine () ! . split ( separator : " " ) . map { Int ( $0 ) ! }
let ( n, m ) = (input [ 0 ] , input [ 1 ] )
cards. append ( contentsOf : readLine () ! .split ( separator : " " ) .map { Int ( $0 ) ! } )
for i in 1 ... n {
prefix . append ( cards [ i ] + **prefix [ i - 1 ] ** )
}
for _ in 1 ... m {
let index = readLine () ! . split ( separator : " " ) . map { Int ( $0 ) ! }
let ( a, b ) = (index [ 0 ] , index [ 1 ] )
print ( prefix [ b ] - prefix [ a - 1 ])
}
Copy print ( [ 1 , 2 , 3 , 4 , 5 , 6 ].prefix ( 3 ))
μλ£κ΅¬μ‘°
Stack
Swiftμ λ°°μ΄ μ체λ₯Ό Stackμ²λΌ μ¬μ©νλ κ²μ΄ λ² μ€νΈμΈλ―! (κ΅³μ΄ λ§λ€ νμ μμ κ² κ°λ€.)
Copy struct Stack < T > {
private var stack: [T] = []
var count: Int {
stack. count
}
var isEmpty: Bool {
stack. isEmpty
}
var peek: T ? {
stack. last
}
mutating func push ( _ element : T) {
stack. append ( element )
}
mutating func pop () -> T ? {
stack. popLast ()
}
}