如何使以下代码更简单?我对Scala很新。提前致谢!
示例1:
def xsum(n: Int): Int = {
if (n<10) n else n%10+xsum(n/10)
}
示例2:
def num(n: Int): Int = {
if (xsum(n)%10==0) n else (100-xsum(n))%10 + n*10
}
def xsum(n: Int): Int = {
if (n<10) n else n%10 + xsum(n/10)
}
示例3:
def tru(n: Int): Boolean = {
n==0 || xsum(n)%10==0
}
def xsum(n: Int): Int = {
if (n<10) n else n%10 + xsum(n/10)
}
答案 0 :(得分:1)
def xsum(n:Int):Int= n.toString.map(_-'0').sum