Algorithm
๋ฐฑ์ค - ์ฃผ์ฌ์๊ฒ์ 2476๋ฒ (Swift)
CheonD
2022. 10. 3. 14:13
๋ฐฑ์ค - ์ฃผ์ฌ์๊ฒ์ 2476๋ฒ (Swift)
๋ฌธ์ ์ค๋ช
https://www.acmicpc.net/problem/2476
2476๋ฒ: ์ฃผ์ฌ์ ๊ฒ์
์ฒซ์งธ ์ค์๋ ์ฐธ์ฌํ๋ ์ฌ๋ ์ N์ด ์ฃผ์ด์ง๊ณ ๊ทธ ๋ค์ ์ค๋ถํฐ N๊ฐ์ ์ค์ ์ฌ๋๋ค์ด ์ฃผ์ฌ์๋ฅผ ๋์ง 3๊ฐ์ ๋์ด ๋น์นธ์ ์ฌ์ด์ ๋๊ณ ๊ฐ๊ฐ ์ฃผ์ด์ง๋ค.
www.acmicpc.net
๋์ ํ์ด
๋ช ๊ฐ์ง ํ ์คํธ์ผ์ด์ค๋ฅผ ์คํจํ๋ ํ์ด์ด๋ค. ์๋ง๋ ๋ฌธ์ ์ ์กฐ๊ฑด์ ์ต๋๊ฐ์์ filter์ map์ ์ฐ์์ผ๋ก ๋๋ ค์ ๊ทธ๋ฐ ๊ฒ ๊ฐ๋ค.
import Foundation
func calculate(_ a: Int, _ b: Int, _ c: Int) -> Int {
var ch = [Int](repeating: 0, count: 7)
ch[a] += 1
ch[b] += 1
ch[c] += 1
if ch.max()! == 3 {
let val = ch.enumerated().filter{ $0.element == 3 }.map{ $0.offset }[0]
return 10000+(val*1000)
} else if ch.max()! == 2 {
let val = ch.enumerated().filter{ $0.element == 2 }.map{ $0.offset }[0]
return 1000+(val*100)
} else {
return ch.enumerated().sorted(by: >)[0].offset * 100
}
}
let n = Int(readLine()!)!
var maxPrice = 0
for _ in 0..<n {
let input = readLine()!
let result = input.components(separatedBy: " ").map{Int($0)!}
let price = calculate(result[0], result[1], result[2])
print(price)
maxPrice = max(maxPrice, price)
}
print(maxPrice)
๋ค๋ฅธ ํ์ด
๋ฌธ์ ๋ 3๊ฐ์ ์ฃผ์ฌ์๋ง ๋๋ฆฌ๊ธฐ ๋๋ฌธ์ 3๊ฐ๋ฅผ if๋ฌธ์ผ๋ก ์ชผ๊ฐ์ ํ์๋ค.
์ด๋ ๊ฒ ํ๋ฉด n๋ฒ๋งํผ๋ง ๋ก์ง์ ๋๋ฆฌ๊ธฐ ๋๋ฌธ์ ์๊ฐ๋ณต์ก๋๊ฐ ํ์ ํ ์ค์ด๋ ๋ค.
import Foundation
let n = Int(readLine()!)!
var maxPrice = 0
for _ in 0..<n {
let input = readLine()!
let result = input.components(separatedBy: " ").map{Int($0)!}
let a = result[0], b = result[1], c = result[2]
var price = 0
if a==b && b==c {
price = 10000+(a*1000)
} else if a==b || a==c {
price = 1000+(a*100)
} else if b==c {
price = 1000+(b*100)
} else {
var maxVal = 0
maxVal = max(a, b)
maxVal = max(maxVal, c)
price = maxVal * 100
}
maxPrice = max(maxPrice, price)
}
print(maxPrice)
ํผ๋๋ฐฑ
๋ฌธ์ ์ ํฌ๊ธฐ๊ฐ ์๋ค๋ฉด ํ๋์ฝ๋ฉ์ผ๋ก ํด๊ฒฐํ๋ ๊ฒ ๋๋ก๋ ์ข๋ค