csharp进阶练习题:数字和数字根的总和【难度:2级】:
在这个kata中,你必须创建一个digital root
函数.
数字根是数字中所有数字的_递归和.给定_n,取_n_的数字之和.如果该值有多个数字,则以这种方式继续减少,直到产生一位数字.这仅适用于自然数.
以下是它的工作原理:
digital_root(16)
=> 1 + 6
=> 7
digital_root(942)
=> 9 + 4 + 2
=> 15 ......
=> 1 + 5
=> 6
digital_root(132189)
=> 1 + 3 + 2 + 1 + 8 + 9
=> 24 ......
=> 2 + 4
=> 6
digital_root(493193)
=> 4 + 9 + 3 + 1 + 9 + 3
=> 29 ......
=> 2 + 9
=> 11 ...
=> 1 + 1
=> 2
digitalRoot(16)
=> 1 + 6
=> 7
digitalRoot(942)
=> 9 + 4 + 2
=> 15 ......
=> 1 + 5
=> 6
digitalRoot(132189)
=> 1 + 3 + 2 + 1 + 8 + 9
=> 24 ......
=> 2 + 4
=> 6
digitalRoot(493193)
=> 4 + 9 + 3 + 1 + 9 + 3
=> 29 ......
=> 2 + 9
=> 11 ...
=> 1 + 1
=> 2
ocaml的
digital_root 16
=> 1 + 6
=> 7
digital_root 942
=> 9 + 4 + 2
=> 15 ......
=> 1 + 5
=> 6
digital_root 132189
=> 1 + 3 + 2 + 1 + 8 + 9
=> 24 ......
=> 2 + 4
=> 6
digital_root 493193
=> 4 + 9 + 3 + 1 + 9 + 3
=> 29 ......
=> 2 + 9
=> 11 ...
=> 1 + 1
=> 2
golang
DigitalRoot(16)
=> 1 + 6
=> 7
DigitalRoot(942)
=> 9 + 4 + 2
=> 15 ......
=> 1 + 5
=> 6
DigitalRoot(132189)
=> 1 + 3 + 2 + 1 + 8 + 9
=> 24 ......
=> 2 + 4
=> 6
DigitalRoot(493193)
=> 4 + 9 + 3 + 1 + 9 + 3
=> 29 ......
=> 2 + 9
=> 11 ...
=> 1 + 1
=> 2
编程目标:
public class Number
{
public int DigitalRoot(long n)
{
// Your awesome code here!
}
}
测试样例:
using NUnit.Framework;
[TestFixture]
public class NumberTest
{
private Number num;
[SetUp]
public void SetUp()
num = new Number();
}
[TearDown]
public void TearDown()
{
num = null;
}
最佳答案(多种解法):
更多关联题目:
csharp进阶练习题:幸运票【难度:2级】–景越C# 经典编程题库,不同难度C# 练习题,适合自学C# 的新手进阶训练
免责申明
本博客所有编程题目及答案均收集自互联网,主要用于供网友学习参考,如有侵犯你的权益请联系管理员及时删除,谢谢
题目收集至https://www.codewars.com/
https://www.codewars.com/kata/sum-of-digits-slash-digital-root