Guest User

Untitled

a guest
Oct 20th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. import Foundation
  2.  
  3. struct ReadInput {
  4.  
  5. private var currentIndex: Int = 0
  6. private var inputArray: [String] = []
  7.  
  8. // 데이터를 배열로 변환
  9. public mutating func readLineToArray() -> [String] {
  10.  
  11. guard let result = readLine() else {
  12. return []
  13. }
  14.  
  15. return result.components(separatedBy: " ")
  16. }
  17.  
  18. // 띄어쓰기 단위로 String 읽기
  19. public mutating func read() -> String {
  20. if inputArray.count == 0 {
  21. inputArray = self.readLineToArray()
  22. }
  23. let result = inputArray[inputArray.index(after: currentIndex-1)]
  24. currentIndex += 1
  25.  
  26. if currentIndex == inputArray.count {
  27. self.inputArray.removeAll()
  28. self.currentIndex = 0
  29. }
  30.  
  31. return result
  32. }
  33.  
  34. // Int 데이터 읽기
  35. public mutating func readInt() -> Int {
  36. guard let result = Int(self.read()) else {
  37. fatalError("Int형 데이터가 아닙니다.")
  38. }
  39.  
  40. return result
  41. }
  42.  
  43. // Double 데이터 읽기
  44. public mutating func readDouble() -> Double {
  45. guard let result = Double(self.read()) else {
  46. fatalError("Double형 데이터가 아닙니다.")
  47. }
  48.  
  49. return result
  50. }
  51. }
  52.  
  53.  
  54. var candidates = Array(repeatElement(0, count: 9))
  55. var finalResult = [Int]()
  56. var data: [Int] = []
  57.  
  58. func checkSumIs100(A: [Int]) -> Bool {
  59. var result = 0
  60. for i in A {
  61. result += i
  62. }
  63. return result == 100
  64. }
  65.  
  66. func Com(n: Int, r: Int) {
  67. if r == 0 {
  68. if checkSumIs100(A: candidates) {
  69. finalResult = candidates
  70. }
  71. return
  72. }
  73. if n < r {
  74. return
  75. }
  76.  
  77. candidates[r-1] = data[n-1]
  78. Com(n: n-1, r: r-1)
  79. Com(n: n-1, r: r)
  80. }
  81.  
  82. var ri = ReadInput()
  83.  
  84. for _ in 1...9 {
  85. let input = ri.readInt()
  86. data.append(input)
  87. }
  88.  
  89. Com(n: 9, r: 7)
  90.  
  91. for i in finalResult.sorted() {
  92. if i != 0 {
  93. print(i)
  94. }
  95. }
Add Comment
Please, Sign In to add comment