Today 12/22
while true {
let input = readLine()!.split(separator: " ").map{Int(String($0))!}.sorted(by: >)
if input == [0,0,0] { break }
if input[0]*input[0] == (input[1]*input[1]) + (input[2]*input[2]) {
print("right")
} else {
print("wrong")
}
}
제곱을 구할 때는 pow(value, Int)
, 제곱근을 구할 때는 sqrt(value)
를 사용하면 된다.
while let input = readLine(), input != "0 0 0" {
let line = input.split(separator: " ").map{Double(String($0))!}.sorted(by: >)
let output = pow(line[0], 2) == pow(line[1], 2) + pow(line[2], 2) ? "right" : "wrong"
print(output)
}
어제 배웠던 부분을 활용해서 if로 끝을 걸러주는 것이 아닌 while 차원에서 걸러서 더 가독성이 좋도록 만들어보았다.
let NM = readLine()!.split(separator: " ").map{Int(String($0))!}
let input = readLine()!.split(separator: " ").map{Int(String($0))!}.sorted(by: <)
var output = 0
for one in 0...NM[0]-3 {
for two in 1...NM[0]-2 {
if two <= one { continue }
for three in 2...NM[0]-1 {
if three <= two { continue }
let result = input[one] + input[two] + input[three]
if result <= NM[1] {
if result > output {
output = result
}
} else { break }
}
}
}
print(output)
같은 카드를 중복하여 선택하는 것을 방지하기 위해 if two <= one { continue }
와 같이 처리하였는데, 좀 바보같은 방법이었다.
let NM = readLine()!.split(separator: " ").map{Int(String($0))!}
let input = readLine()!.split(separator: " ").map{Int(String($0))!}.sorted(by: <)
var output = 0
for one in 0...NM[0]-3 {
for two in **one**+1...NM[0]-2 {
for three in **two**+1...NM[0]-1 {
let sumOfCards = input[one] + input[two] + input[three]
if sumOfCards <= NM[1] {
output = max(sumOfCards, output)
}
}
}
}
print(output)
구글링을 하면서 배워올 여러가지를 좀 적용했다.
for two in one+1...NM[0]-2
같이 one, two, three 변수를 사용해서 for문을 만들었다.sumOfCards
와 같이 명료하게 알아볼 수 있는 변수명으로 변수를 생성하였다.output = max(sumOfCards, output)
와 같이 한 줄로 나타낼 수 있게했다.let input = Int(readLine()!)!
var seriesArr: [String] = []
for index in 0...3000 {
let indexArr = String(index).map{String($0)}
if indexArr.last == "6" {
if indexArr.count >= 2, indexArr[indexArr.count-2] == "6" {
if indexArr.count >= 3, indexArr[indexArr.count-3] == "6" {
// 666 일 때
if indexArr.count == 3 {
for sixHunSixtySix in 0...999 {
if sixHunSixtySix < 10 {
seriesArr.append("66600"+String(sixHunSixtySix))
} else if sixHunSixtySix >= 10, sixHunSixtySix < 100 {
seriesArr.append("6660"+String(sixHunSixtySix))
} else if sixHunSixtySix >= 100 {
seriesArr.append("666"+String(sixHunSixtySix))
}
}
} else if indexArr.count == 4 {
for sixHunSixtySix in 0...999 {
if sixHunSixtySix < 10 {
seriesArr.append(indexArr[0]+"66600"+String(sixHunSixtySix))
} else if sixHunSixtySix >= 10, sixHunSixtySix < 100 {
seriesArr.append(indexArr[0]+"6660"+String(sixHunSixtySix))
} else if sixHunSixtySix >= 100 {
seriesArr.append(indexArr[0]+"666"+String(sixHunSixtySix))
}
}
}
} else {
// 66일 때
if indexArr.count == 2 {
for sixtySix in 0...99 {
if sixtySix < 10 {
seriesArr.append("6660"+String(sixtySix))
} else {
seriesArr.append("666"+String(sixtySix))
}
}
} else if indexArr.count == 3 {
for sixtySix in 0...99 {
if sixtySix < 10 {
seriesArr.append(indexArr[0]+"6660"+String(sixtySix))
} else {
seriesArr.append(indexArr[0]+"666"+String(sixtySix))
}
}
} else if indexArr.count == 4 {
for sixtySix in 0...99 {
if sixtySix < 10 {
seriesArr.append(indexArr[0]+indexArr[1]+"6660"+String(sixtySix))
} else {
seriesArr.append(indexArr[0]+indexArr[1]+"666"+String(sixtySix))
}
}
}
}
} else {
// 6일 때
if indexArr.count == 1 {
for six in 0...9 {
seriesArr.append("666"+String(six))
}
} else if indexArr.count == 2 {
for six in 0...9 {
seriesArr.append(indexArr[0]+"666"+String(six))
}
} else if indexArr.count == 3 {
for six in 0...9 {
seriesArr.append(indexArr[0]+indexArr[1]+"666"+String(six))
}
} else if indexArr.count == 4 {
for six in 0...9 {
seriesArr.append(indexArr[0]+indexArr[1]+indexArr[2]+"666"+String(six))
}
}
}
} else {
seriesArr.append(String(index)+"666")
}
}
seriesArr[0] = "666"
print(seriesArr[input-1])
1부터 하나하나 올라가는건 말이 안될 것 같아서 369게임처럼 6일 때, 66일 때, 666일 때 경우를 모두 나눠서 몇 번째 시리즈 제목에는 어떤 숫자가 올지 유추했다.
또, 항상 수를 구하는 것보다 배열에 한 번 저장해두고 꺼내오는게 더 효율적일 것 같아서 배열을 이용했다.
결론적으로는 1부터 올라가는 것보다는 훨씬 빠른 시간이 걸렸다. 근데 풀이들을 보니 그냥 1부터 올라가는 것 같다. (배열도 이용 안하고)
let input = Int(readLine()!)!
var number = 666
var count = 0
while true {
var sixCount = 0
var numberInstance = number
while numberInstance != 0 {
if numberInstance % 10 == 6 {
sixCount += 1
} else {
sixCount = 0
}
if sixCount == 3 {
count += 1
break
}
numberInstance /= 10
}
if count == input {
print(number)
break
}
number += 1
}
그래서 다시 풀어봤다.
핵심 개념은 Int number % 10, number / 10
을 반복해서 count를 세서 연속된 6을 찾아내는 것.