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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_45444821/article/details/102716716

csharp基础练习题:最接近零【难度:1级】:

只需从列表中查找最接近的值为零.请注意,有列表中的底片.

名单始终是不是空的,并且只包含整数.返回

如果它不可能只定义这样的一个值.当然,我们期待0作为最接近的值为零.

例子:

代码
[2,4,-1,-3] => -1
[5,2,-2] =>无
[5,2,2] => 2
[13,0,-6] => 0

编程目标:

public class Kata
{
  public static int? Closest(int[] arr)
  {
    // return null if it is not possible to return 1 unique closest value
    return 0;
  }
}


测试样例:

namespace Solution {
  using NUnit.Framework;
  using System;
  using System.Collections.Generic;
  [TestFixture]
  public class SolutionTest
  {
      get
      {
        yield return new TestCaseData(new int[] {10, 3, 9, 1}).Returns(1).SetDescription("Simple test");
        yield return new TestCaseData(new int[] {2, 4, -1, -3}).Returns(-1).SetDescription("Simple test");
        yield return new TestCaseData(new int[] {13, 0, -6}).Returns(0).SetDescription("Simple test");
        yield return new TestCaseData(new int[] {13, 0, 0, -6}).Returns(0).SetDescription("Simple test");
      }


最佳答案(多种解法):

点击查看答案

更多关联题目:

csharp基础练习题:计数像一个孩子.【难度:1级】–景越C# 经典编程题库,不同难度C# 练习题,适合自学C# 的新手进阶训练

免责申明

本博客所有编程题目及答案均收集自互联网,主要用于供网友学习参考,如有侵犯你的权益请联系管理员及时删除,谢谢
题目收集至https://www.codewars.com/
https://www.codewars.com/kata/closest-to-zero

猜你喜欢

转载自blog.csdn.net/weixin_45444821/article/details/102716716