csharp基础练习题:序列的总和【难度:1级】:
你的任务是使函数返回整数序列的总和.
该序列由3非负值定义:** ** 开始,** ** 端,** ** 步骤.
如果** ** 开始值大于结束** ** 越大,函数应返回** 0 **
- 例子*
```javascript
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
```
```php
sequence_sum(2,2,2); // => 2
sequence_sum(2,6,2); // => 12(= 2 + 4 + 6)
sequence_sum(1,5,1); // => 15(= 1 + 2 + 3 + 4 + 5)
sequence_sum(1,5,3); // => 5(= 1 + 4)
```
```haskell
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
```
```csharp
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
```
```cpp
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); / * => 15 - > 1 + 2 + 3 + 4 + 5 * /
sequence_sum(1,5,3); / * => 5 - > 1 + 4 * /
```
MOV EDI,2
MOV ESI,2
MOV EDX,2
调用sequence_sum; EAX < - 2
MOV EDI,2
MOV ESI,6
MOV EDX,2
调用sequence_sum; EAX < - 12 = 2 + 4 + 6
MOV EDI,1
MOV ESI,5
MOV EDX,1
调用sequence_sum; EAX < - 15 = 1 + 2 + 3 + 4 + 5
MOV EDI,1
MOV ESI,5
MOV EDX,3
调用sequence_sum; EAX < - 5 = 1 + 4
这是该系列的第一习题:
1)的序列的总和(这习题)
2)之和一个序列[硬核版]
编程目标:
public static class Kata
{
public static int SequenceSum(int start, int end, int 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(45, Kata.SequenceSum(0, 15, 3));
Assert.AreEqual(0, Kata.SequenceSum(16, 15, 3));
Assert.AreEqual(26, Kata.SequenceSum(2, 24, 22));
Assert.AreEqual(2, Kata.SequenceSum(2, 2, 2));
Assert.AreEqual(2, Kata.SequenceSum(2, 2, 1));
最佳答案(多种解法):
更多关联题目:
csharp高级练习题:让我们玩飞镖!【难度:3级】–景越C# 经典编程题库,不同难度C# 练习题,适合自学C# 的新手进阶训练
免责申明
本博客所有编程题目及答案均收集自互联网,主要用于供网友学习参考,如有侵犯你的权益请联系管理员及时删除,谢谢
题目收集至https://www.codewars.com/
https://www.codewars.com/kata/sum-of-a-sequence