// 4.2 数组中只有一个数字,即停止,调用判断相等方法判断是否相等 if cards.count ==1 { let result = isEqual24(cards[0]) return result }
let count = cards.count // 4.3 从数组中依次取两个数字,两个数字不能相等 for i in0..<count { for j in0..<count { if i != j { let a = cards[i] let b = cards[j]
// 4.4 把余下的数字放入新的数组中 var restCards: [Double] = [] for k in0..<count { if k != i && k != j { restCards.append(cards[k]) } }
// 4.5 遍历运算符数组 for op inOperationList { // 4.5.1 运算符为"+"或"*"时,注意交换律,刚开始 i < j,所以到 i > j 时,就不需要重复计算了 if ((op =="+"|| op =="*") && (i > j)) { // "+"、"*", a + b = b + a, no need to recalculate continue }
// 4.5.2 运算符为"-"时,除数不能为0 if (op =="/") && b < elipson { // "/" dividend can not equal to zero continue }
// 4.2 数组中只有一个数字,即停止,调用判断相等方法判断是否相等 if cards.count ==1 { let result = isEqual24(cards[0]) return (result, resultExpressList) } // 将传入数据变为可变数组 var expressionList: [String] = [] expressionList.append(contentsOf: resultExpressList)
let count = cards.count // 4.3 从数组中依次取两个数字,两个数字不能相等 for i in0..<count { for j in0..<count { if i != j { let a = cards[i] let b = cards[j]
// 4.4 把余下的数字放入新的数组中 var restCards: [Double] = [] for k in0..<count { if k != i && k != j { restCards.append(cards[k]) } }
// 4.5 遍历运算符数组 for op inOperationList { // 4.5.1 运算符为"+"或"*"时,注意交换律,刚开始 i < j,所以到 i > j 时,就不需要重复计算了 if ((op =="+"|| op =="*") && (i > j)) { // "+"、"*", a + b = b + a, no need to recalculate continue }
// 4.5.2 运算符为"-"时,除数不能为0 if (op =="/") && b < elipson { // "/" dividend can not equal to zero continue }
funcgenerateExpressStr(fromlist: [String]) -> String { var resultStrList: [NSMutableDictionary] = [] let strKey ="expressionStr" let valueKey ="expressionValue" let statusKey ="expressionUsed"
var newResultStrList: [NSMutableDictionary] = [] for index in0..<list.count { let itemStr = list[index] let componentList = itemStr.components(separatedBy: " = ") var expressionStr = componentList[0] let expressionValue = componentList[1] if index ==0 { let tempDic =NSMutableDictionary() tempDic.setValue(expressionStr, forKey: strKey) tempDic.setValue(expressionValue, forKey: valueKey) tempDic.setValue("0", forKey: statusKey) newResultStrList.append(tempDic) } else { for itemDic in resultStrList { print(itemDic) iflet previousExpressionStr = itemDic.value(forKey: strKey) as?String, let previousExpressionValueStr = itemDic.value(forKey: valueKey) as?String, let previousStatusValue = itemDic.value(forKey: statusKey) as?String, previousStatusValue =="0" { let tempDic =NSMutableDictionary() if expressionStr.contains(previousExpressionValueStr) { let range = (expressionStr asNSString).range(of: previousExpressionValueStr) let newExpressionStr = (expressionStr asNSString).replacingCharacters(in: range, with: previousExpressionStr) expressionStr = newExpressionStr let newExpressionValue = expressionValue tempDic.setValue(newExpressionStr, forKey: strKey) tempDic.setValue(newExpressionValue, forKey: valueKey) tempDic.setValue("0", forKey: statusKey) newResultStrList.append(tempDic) itemDic.setValue("1", forKey: statusKey) } else { let newExpressionStr = expressionStr let newExpressionValue = expressionValue let tempDic =NSMutableDictionary() tempDic.setValue(newExpressionStr, forKey: strKey) tempDic.setValue(newExpressionValue, forKey: valueKey) tempDic.setValue("0", forKey: statusKey) newResultStrList.append(tempDic) } } } } resultStrList = newResultStrList } let resultStr = resultStrList.last?.value(forKey: strKey) as?String??"" print(resultStr) return resultStr }