๋ฐฑ์ค - ์ฃผ์ฌ์๊ฒ์ 2476๋ฒ (Swift)
2022. 10. 3. 14:13ใAlgorithm
๋ฐฑ์ค - ์ฃผ์ฌ์๊ฒ์ 2476๋ฒ (Swift)
๋ฌธ์ ์ค๋ช
https://www.acmicpc.net/problem/2476
๋์ ํ์ด
๋ช ๊ฐ์ง ํ ์คํธ์ผ์ด์ค๋ฅผ ์คํจํ๋ ํ์ด์ด๋ค. ์๋ง๋ ๋ฌธ์ ์ ์กฐ๊ฑด์ ์ต๋๊ฐ์์ 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)
ํผ๋๋ฐฑ
๋ฌธ์ ์ ํฌ๊ธฐ๊ฐ ์๋ค๋ฉด ํ๋์ฝ๋ฉ์ผ๋ก ํด๊ฒฐํ๋ ๊ฒ ๋๋ก๋ ์ข๋ค
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค - ํ๋ฌธ(Swift) (0) | 2022.10.06 |
---|---|
ํ๋ฌธ ๋ฌธ์์ด ๊ฒ์ฌ(Swift) (1) | 2022.10.05 |
์์ฃผํ์ง ๋ชปํ ์ ์ - ํ๋ก๊ทธ๋๋จธ์ค(python3) (0) | 2022.10.03 |
๋ํ๊ฐ (2) | 2022.10.01 |
K๋ฒ์งธ ํฐ ์(Swift) (0) | 2022.09.30 |