Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Tags
more
Archives
Today
Total
관리 메뉴

Olive Study Room

[백준 알고리즘] 5. 1차원 배열 본문

cs/알고리즘

[백준 알고리즘] 5. 1차원 배열

Olive Dev 2021. 6. 20. 23:55
 10818
 오류
 다시 풀어보기
let line = Int(readLine()!)!
var num = readLine()!.split(separator: " ").map({Int($0)!})
var maxNum: Int = -1000000
var minNum: Int = 1000000

for i in 0...num.count-1 {
    if num[i] > maxNum {
        maxNum = num[i]
    }
    if num[i] < minNum {
        minNum = num[i]
    }
}
print("\(minNum) \(maxNum)")

 10818 - 2
let line = Int(readLine()!)!
var num = readLine()!.split(separator: " ").map({Int(String($0))!})
print("\(num.min()!) \(num.max()!)")

 2562
var arr: [Int] = []
var maxNum: Int = 0
var maxNumIndex: Int = 0
for i in 1...9 {
    let line = Int(readLine()!)!
    arr.append(line)
    if line > maxNum {
        maxNum = line
        maxNumIndex = i
    }
}
print(arr.max()!)
print(maxNumIndex)

 2577
 다시 풀어보기

var value: Int = 1
var arr: [Int] = Array.init(repeating: 0, count: 10)
for _ in 1...3 {
    let line = Int(readLine()!)!
    value *= line
}
var result = String(value)

let arr2 = Array(result).map({String($0)})
arr2.forEach({
    for i in 0...9 {
        if $0 == String(i) {
            arr[i] += 1
        }
    }
})
arr.forEach( {
    print($0)
})

 2577 - 2
var value: Int = 1
var arr: [Int] = Array.init(repeating: 0, count: 10)
for _ in 1...3 {
    let line = Int(readLine()!)!
    value *= line
}
while value != 0 {
    let index = value%10
    arr[index] += 1
    value /= 10
}
arr.forEach( {
    print($0)
})

3052
var arr2: Set<Int> = []
for _ in 0...9 {
    let line = Int(readLine()!)!
    arr2.insert(line%42)
}
print(arr2.count)

1546
let line = Int(readLine()!)!
let score = readLine()!.split(separator: " ").map{Double($0)!}
var fixedScore: [Double] = []
var sum: Double = 0.0
score.forEach({
    fixedScore.append($0/score.max()!*100)
})
for i in 0...fixedScore.count-1 {
    sum += fixedScore[i]
}
print(String(sum/Double(fixedScore.count)))

 1546 - 2
let line = Int(readLine()!)!
let score = readLine()!.split(separator: " ").map{Double($0)!}
var fixedScore: [Double] = []
var sum: Double = 0.0

fixedScore = score.map({$0/score.max()!*100})
sum = fixedScore.reduce(0, {$0 + $1})
print(String(sum/Double(fixedScore.count)))

8958
 런타임에러
let line = Int(readLine()!)!
for _ in 1...line {
    var score: Int = 0
    let result = readLine()!.split(separator: "X").map({String($0)})
    for i in 0...result.count-1 {
        var scorePlus: Int = 1
        result[i].forEach({_ in
            score += scorePlus
            scorePlus += 1
        })
    }
    print(score)
}

 8958 - 2
 가우스 함수
 reduce

let line = Int(readLine()!)!
for _ in 1...line {
    print(readLine()!.split(separator: "X").map{ $0.count * ($0.count+1)/2}.reduce(0, +))
}

 4344
import Foundation
let line = Int(readLine()!)!
for _ in 0..<line {
    let line2 = readLine()!.split(separator: " ").map{ Double($0)! }
    var score: [Double] = []
    for j in 1...line2.count-1 {
        score.append(line2[j])
    }
    let sum = score.reduce(0, {$0 + $1})
    print("\(String(format: "%.3f", Double(score.filter({sum/line2[0] < $0}).count)/line2[0] * 100))%")
}

 4344 - 2
import Foundation
let line = Int(readLine()!)!
for _ in 0..<line {
    let line2 = readLine()!.split(separator: " ").map{ Double($0)! }
    var score: [Double] = []
    for j in 1...line2.count-1 {
        score.append(line2[j])
    }
    let sum = score.reduce(0, {$0 + $1})
    print("\(String(format: "%.3f%%", Double(score.filter({sum/line2[0] < $0}).count)/line2[0] * 100))%")
}

 

Comments