-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path067.swift
More file actions
56 lines (51 loc) · 1002 Bytes
/
067.swift
File metadata and controls
56 lines (51 loc) · 1002 Bytes
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import Foundation
let nk = readLine()!.split(separator: " ")
let nStr = nk[0]
let k = Int(nk[1])!
//与えられた文字列を8進数で解釈する
var value = 0
for c in nStr {
value *= 8
let number = Int(String(c))!
value += number
}
//操作
for _ in 1...k {
value = to9andChange8to5(value: value)
}
//8進数で出力
var printON = false
for i in (0...19).reversed() {
//let dividing = Int(pow(Double(8), Double(i)))
var dividing = 1
for _ in 0..<i {
dividing *= 8
}
if value / dividing > 0 {
printON = true
}
if printON {
print(value / dividing, terminator: "")
}
value %= dividing
}
if printON {
print("")
} else {
print("0") //与えられた数が0のとき
}
func to9andChange8to5(value: Int) -> Int {
var current = value
var returnValue = 0
var multipleValue = 1
while current > 0 {
var remainder = current % 9
if remainder == 8 {
remainder = 5
}
returnValue += remainder * multipleValue
multipleValue *= 8
current /= 9
}
return returnValue
}