ios - Swift 错误,无法将类型 Int 转换为 int 16

标签 ios swift

此函数将数字与该数字分量的立方之和进行比较。例如 abc=a^3 + b^3 + c^3。转换出错,请帮忙。

func triKuba ( i:Int16, k:Int16, var array:[Int16]=[] ) ->Int16{

    for var i=100;i<1000; i++ {
        array.append(Int16(i))
        if array[i] == pow(array[i]/10) + pow(array[i]/100) + pow(array[i]%10)  {
            return array[i]
        } else {
            return 0
        }
    }
}
 triKuba(0, k: 0)

如果我理解正确,方法 pow 周围的行中的下一个错误“无法使用 Int16 类型的参数列表调用 pow”,方法 pow 是一个 ^3

最佳答案

我强烈怀疑这就是您要查找的内容:

func arithmeticRoot3(var value: Int) -> Int {
    var result = 0

    while value > 0 {
        let digit = value % 10
        result += digit * digit * digit
        value /= 10
    }

    return result
}

func triKuba() -> [Int] {
    return (100...999).filter() {
        $0 == arithmeticRoot3($0)
    }
}

print(triKuba()) // [153, 370, 371, 407]

关于ios - Swift 错误,无法将类型 Int 转换为 int 16,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35855494/

相关文章:

iphone - 如何更改 UITableViewCell 选定 View 的颜色?

iphone - 径向渐变绘制性能 - OpenGL-ES 可以改进吗?

ios - Swift 中 navigationControllerSupportedInterfaceOrientations 的返回值是什么?

ios - 接收有关任何子更改的通知

swift - 从 collectionView 的选定项填充 tableview

ios - 在我的例子中,ISO 时间戳应该使用什么格式

html - CSS 浏览器 : Absolute Bottom/Left not working as expected

iOS:如何备份项目?

ios - graph.facebook.com - 传输安全 block

swift 语法使用 UITouch 删除图像