csharp基础练习题:序列的总和【难度:1级】--景越C#经典编程题库,不同难度C#练习题,适合自学C#的新手进阶训练

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值