알고리즘/백준
[백준 swift] 1차원 배열 - 1546번, 4344번
Diana_iOS
2021. 4. 1. 17:12
1546번
import Foundation
var num = Double(readLine()!)!
var scores = readLine()!.components(separatedBy: " ").map{Double($0)!}
var max = scores.max()!
var modifiedScores = scores.map{($0/max)*100}.reduce(0, {$0 + $1})
print(modifiedScores/num)
4344번
import Foundation
var C = Int(readLine()!)!
for _ in 1...C {
var testCase = (readLine() ?? "").components(separatedBy:" ").map{Int($0)!}
let num = testCase[0]
testCase.removeFirst()
let sum = testCase.reduce(0,{$0 + $1})
let average = sum / num
let count = Float(testCase.filter{$0 > average}.count)
let rate = (Float(count) / Float(testCase.count)) * 100
let result = String(format: "%.3f%%", rate)
print(result)
}
String format 에서 퍼센트기호(%)를 사용하기 위해서는 퍼센트기호를 두 번 써줘야 한다.