正如标题所暗示的,这是的硬核版另一整齐习题一>.
任务很简单解释:简单地从第一个参数是开始到第二个参数是上限(可能包括)总结所有号码,将在通过第三个参数表示的步骤:
sequenceSum(2,2,2)=== 2
sequenceSum(2,6,2)=== 12 // 2 + 4 + 6
sequenceSum(1,5,1)=== 15 // 1 + 2 + 3 + 4 + 5
sequenceSum(1,5,3)=== 5 // 1 + 4
sequence_sum(2,2,2)#2
sequence_sum(2,6,2)#12(= 2 + 4 + 6)
sequence_sum(1,5,1)#1(= 1 + 2 + 3 + 4 + 5)
sequence_sum(1,5,3)#5(= 1 + 4)
sequence_sum(2,2,2)#2
sequence_sum(2,6,2)#12(= 2 + 4 + 6)
sequence_sum(1,5,1)#1(= 1 + 2 + 3 + 4 + 5)
sequence_sum(1,5,3)#5(= 1 + 4)
晶
sequence_sum(2,2,2)#2
sequence_sum(2,6,2)#12(= 2 + 4 + 6)
sequence_sum(1,5,1)#1(= 1 + 2 + 3 + 4 + 5)
sequence_sum(1,5,3)#5(= 1 + 4)
SequenceSum(2,2,2)#2
SequenceSum(2,6,2)#12(= 2 + 4 + 6)
SequenceSum(1,5,1)#1(= 1 + 2 + 3 + 4 + 5)
SequenceSum(1,3,5)#1
如果它是不可能的序列(期初为较大的结束和一个积极的步骤,或者反过来),刚刚返回0
.请参见所提供的测试案例进行进一步的例子:)
** 注:** 从其他基地习题不同,更大的范围将受到考验,所以你应该希望得到您的算法中进行了优化,以避免暴力破解的方式通过该解决方案.
using System;
public static class Kata
{
public static long SequenceSum(long start, long end, long step)
{
return -1;
}
}
namespace Solution {
using NUnit.Framework;
using System;
[TestFixture]
public class SolutionTest
{
[Test]
public void BasicTest()
Assert.AreEqual(12,Kata.SequenceSum(2, 6, 2));
Assert.AreEqual(15,Kata.SequenceSum(1, 5, 1));
Assert.AreEqual(5,Kata.SequenceSum(1, 5, 3));
Assert.AreEqual(-5,Kata.SequenceSum(-1, -5, -3));
Assert.AreEqual(0,Kata.SequenceSum(16, 15, 3));
Assert.AreEqual(-26,Kata.SequenceSum(-24, -2, 22));
Assert.AreEqual(-2,Kata.SequenceSum(-2, 4, 658));
Assert.AreEqual(469436517521190,Kata.SequenceSum(780, 68515438, 5));
点击查看答案
csharp基础练习题:欢迎来到这个城市【难度:0级】–景越C# 经典编程题库,不同难度C# 练习题,适合自学C# 的新手进阶训练
csharp进阶练习题:复活节【难度:2级】–景越C# 经典编程题库,不同难度C# 练习题,适合自学C# 的新手进阶训练
本博客所有编程题目及答案均收集自互联网,主要用于供网友学习参考,如有侵犯你的权益请联系管理员及时删除,谢谢
题目收集至https://www.codewars.com/
https://www.codewars.com/kata/sum-of-a-sequence-hard-core-version