feldblume5263 / TIL

Today I Learned
0 stars 0 forks source link

(220921)[개인 공부] 백준 (1시간) #34

Closed feldblume5263 closed 2 years ago

feldblume5263 commented 2 years ago

잘해보자~!@@~

feldblume5263 commented 2 years ago

1712

let i = readLine()!.split(separator: " ").map{ Int($0)! }
var sum = i[0] + i[1]
if i[2] - i[1] == 0 {
    print(-1)
} else {
    let res = (i[0] / (i[2] - i[1])) + 1
    if res < 0 { print(-1) } else { print(res) }
}

2792

let n = Int(readLine()!)!
func cal(k: Int, idx: Int) {
    if k >= n {
        print(idx)
        return
    }
    let newIdx = idx + 1
    let newK = k + (6 * idx)
    cal(k: newK, idx: newIdx)
}
cal(k: 1, idx: 1)

1193

let n = Int(readLine()!)!

var idx = 0
var sum = 1
var saved = 0
while true {
    if sum > n { break }
    idx += 1
    saved = sum
    sum += idx
}

let d = n - saved + 1

if idx % 2 == 0 {
    print("\(d)/\(idx + 1 - d)")
} else {
    print("\(idx + 1 - d)/\(d)")
}

2869

import Foundation
let i = readLine()!.split(separator: " ").map{ Double($0)! }
let d = ceil(i[2] / (i[0] - i[1]))
if ((ceil(i[0] - i[1]) * (d - 1)) + i[1]) >= i[2] {
    let plus = (((ceil(i[0] - i[1]) * (d - 1)) + i[1]) - i[2])
    print(Int(d - 1) - Int(plus / (i[0] - i[1])))
} else {
    print(Int(d))
}

10250

import Foundation
let n = Int(readLine()!)!
var res = [String]()
for _ in 0 ..< n {
    let l = readLine()!.split(separator: " ").map{ Double($0)! }
    var f = String(Int(l[2]) % Int(l[0]))
    var r = String(Int(ceil(l[2] / l[0])))
    if f == "0" {f = String(Int(l[0]))}
    if r.count == 1 {r = "0" + r}
    res.append(f + r)
}
for re in res {
    print(re)
}

2775

let n = Int(readLine()!)!

var res = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]]

for f in 1..<15 {
    var new = [Int]()
    for r in 0..<14 {
        var sum = 0
        for e in 0...r {
            sum += res[f - 1][e]
        }
        new.append(sum)
    }
    res.append(new)
}
for _ in 0..<n {
    let f = Int(readLine()!)!
    let r = Int(readLine()!)!
    print(res[f][r - 1])
}

2839

var input = Int(readLine()!)!
var n = input
var five = 0
var three = 0

while n > 0 {
    n -= 5
    five += 1
}
if n < 0 {
    while n != 0 && n <= input {
        if five > 0 && n <= input {
            five -= 1
            n += 5
        }
        while n > 0 {
            n -= 3
            three += 1
        }
        if n < 0 {
            n += (three * 3)
            three = 0
        } else {
            break
        }
        if five == 0 {
            break
        }
    }
}
if n != 0 {
    print(-1)
} else {
    print(five + three)
}

10757

func plus(a: String, b: String) -> String {
    var newA = a.reversed().map{String($0)}
    var newB = b.reversed().map{String($0)}
    var res = [String]()
    let count = newA.count > newB.count ? newA.count : newB.count
    for _ in 0 ..< count - newA.count {
        newA.append("0")
    }
    for _ in 0 ..< count - newB.count {
        newB.append("0")
    }
    var plusFlag = false
    for idx in 0..<count {
        var counted = Int(newA[idx])! + Int(newB[idx])!
        if plusFlag == true {
            counted += 1
            plusFlag = false
        }
        if counted >= 10 {
            plusFlag = true
            counted -= 10
        }
        res.append(String(counted))
    }
    if plusFlag == true {
        res.append("1")
    }
    return res.reversed().joined()
}

let a = readLine()!.split(separator: " ").map{String($0)}
print(plus(a: a[0], b: a[1]))

1978

_ = readLine()!
let ns = readLine()!.split(separator: " ").map{Int($0)!}
let biggest = ns.max()!
var res = [2]
for n in 2...biggest {
    if res.contains(n) == false {
        var flag = true
        for re in res {
            if n % re == 0 {
                flag = false
                break
            }
        }
        if flag {
            res.append(n)
        }
    }
}
var count = 0
for n in ns {
    if res.contains(n) {
        count += 1
    }
}
print(count)

2581

let small = Int(readLine()!)!
let big = Int(readLine()!)!
var res = [2]
if big >= 3 {
    for num in 3 ... big {
        var flag = true
        for re in res {
            if num % re == 0 {
                flag = false
                break
            }
        }
        if flag {
            res.append(num)
        }
    }
}
var count = 0
var smallest = 0
for re in res {
    if re >= small && re <= big {
        if count == 0 {
            smallest = re
        }
        count += re
    }
}
if count > 0 { print(count) } else { print(-1) }
if smallest != 0 {
    print(smallest)
}

11653

var n = Int(readLine()!)!
var idx = 2
while n != 1 {
    if n % idx == 0 {
        n /= idx
        print(idx)
    } else {
        idx += 1
    }
}

1929

let a = readLine()!.split(separator: " ").map{Int($0)!}
var arr = Array(repeating: 0, count: a[1] + 1)

for idx in 2...a[1] {
    arr[idx] = idx
}

for j in 2...a[1] {
    if arr[j] == 0 {continue}
    for k in stride(from: j + j, through: a[1], by: j) {
        arr[k] = 0
    }
}

for w in a[0]...a[1] {
    if arr[w] != 0 {
        print("\(arr[w])")
    }
}