Algorithm/Baekjoon Online Judge
1110번 '더하기 사이클'
할루루
2018. 2. 22. 11:37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | let input = Int(readLine()!)! var count = 0 var temp: Int = input var tempToString = String(temp) repeat{ if(temp < 10){ tempToString = "0"+tempToString } let sum = Int(tempToString[tempToString.startIndex].description)! + Int(tempToString[tempToString.index(tempToString.startIndex, offsetBy: 1)].description)! let sumToString = String(sum) let newNumber: Int if(sum < 10){ newNumber = Int(tempToString[tempToString.index(tempToString.startIndex, offsetBy: 1)].description + sumToString[sumToString.index(sumToString.startIndex, offsetBy: 0)].description)! } else{ newNumber = Int(tempToString[tempToString.index(tempToString.startIndex, offsetBy: 1)].description + sumToString[sumToString.index(sumToString.startIndex, offsetBy: 1)].description)! } temp = newNumber tempToString = String(newNumber) count += 1 } while(temp != input) print(count) | cs |