C#练习题答案: 惯性阵【难度:1级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

惯性阵【难度:1级】:

答案1:

using System;
using System.Linq;

public class Kata
{
  public static bool IsInertial(int[] arr) {
    var inertialTester = new InertialTester(arr);
    return inertialTester.IsInertial();
  }
}

public class InertialTester
{
  private readonly int[] _input;

  public InertialTester(int[] input)
  {
    _input = input;
    Array.Sort(_input);
  }

  public bool IsInertial()
  {
    return HasElements() &&
          ContainsAtLeastOneOddValue() &&
          MaximumValueIsEven() &&
        EveryOddIsGreaterThanEveryEvenExceptMaximum();
  }

  private bool HasElements()
  {
    return _input != null && _input.Length > 0;
  }

  private bool EveryOddIsGreaterThanEveryEvenExceptMaximum()
  {
    var maximum = _input.Max();
    return _input.Where(IsOdd).Min() > _input.Where(value => IsEven(value) && (value != maximum)).Max();
  }

  private bool MaximumValueIsEven()
  {
    return IsEven(_input.Max());
  }

  private static bool IsEven(int input)
  {
    return input % 2 == 0;
  }

  private static bool IsOdd(int input)
  {
    return !IsEven(input);
  }

  private bool ContainsAtLeastOneOddValue()
  {
    return _input.Any(IsOdd);
  }
}

答案2:

using System.Linq;

public class Kata
{
  public static bool IsInertial(int[] arr) 
  { 
      var max = arr.Max();
      if (max%2 != 0) return false;
      if (!arr.Any(x => x%2 != 0)) return false;
      if (arr.Where(x => x%2 != 0).Any(x => arr.Where(y => y%2 == 0).Any(y => y != max && y > x))) return false; 
      return true; 
  }
}

答案3:

using System;
using System.Linq;

public class Kata
{
  public static Boolean IsInertial(Int32[] arr) =>
    arr.Length > 0 && 
    arr.Any(v => v % 2 != 0) &&
    arr.Max() % 2 == 0 &&
    arr.All(v => v % 2 == 0 || v > arr.Where(val => val != arr.Max() && val % 2 == 0).DefaultIfEmpty(Int32.MinValue).Max());
}

答案4:

using System;
using System.Linq;

public class Kata
{
  public static bool IsInertial(int[] arr) {
    var inertialTester = new InertialTester(arr);

    return inertialTester.IsInertial();
  }
}

public class InertialTester
{
  private readonly int[] _input;

  public InertialTester(int[] input)
  {
    _input = input;
    Array.Sort(_input);
  }

  public bool IsInertial()
  {
    if (_input.Length == 0)
    {
      return false;
    }

    Array.Sort(_input);

    if (!IsEven(_input.Last()))
    {
      return false;
    }

    var foundOdd = false;

    for (var index = 0; index < _input.Length - 1; index++)
    {
      var isEven = IsEven(_input[index]);
      if (isEven)
      {
        if (foundOdd)
        {
          return false;
        }
        continue;
      }
      foundOdd = true;
    }

    return foundOdd;
  }

  private static bool IsEven(int input)
  {
    return input % 2 == 0;
  }
}

答案5:

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
{
    public static Boolean IsInertial(Int32[] arr)
    {
        var flag = false;

        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i] % 2 != 0)
            {
                flag = true;
                break;
            }
        }

        if (!flag)
            return false;

        if (arr.Max() % 2 != 0)
            return false;

        List<int> evenList = new List<int>();
        List<int> oddList = new List<int>();

        foreach (var val in arr)
        {
            if (val % 2 == 0)
                evenList.Add(val);
            else
                oddList.Add(val);
        }

        var max = evenList.Max();
        var remov = evenList.Remove(max);

        foreach (var odd in oddList)
        {
            foreach (var even in evenList)
            {
                if (even > odd)
                    return false;
            }
        }

        return true;
    }
}

答案6:

using System;
using System.Linq;

public class Kata {
    public static Boolean IsInertial( Int32[] arr ) {
        if ( arr.All( n => n%2 == 0 ) ) {
            return false;
        }
        var max = arr.Max( );
        if ( max%2 != 0 ) {
            return false;
        }
        var even = arr.Where( n => n%2 == 0 &amp;&amp; n != max ).ToArray( );
        var odd = arr.Where( n => n%2 != 0 ).ToArray( );
        return odd.All( n => !even.Any( e => e >= n ) );
    }
}

答案7:

using System;
using System.Linq;

public class Kata
{
  public static Boolean IsInertial(Int32[] arr)
  {
    int minOdd; int maxEven; int max = arr.Max();
    if (max%2!=0) { return false; }
    try
    {
      minOdd = arr.Where(x => x%2!=0).Min();
      maxEven = arr.Where(y => y%2==0 &amp;&amp; y<max).Max();
    }
    catch (InvalidOperationException)
    { 
      return false;
    }
    return minOdd > maxEven;
  }
}

答案8:

using System.Linq;

public class Kata
{
  /// <summary>
  /// Returns a Boolean representing if the passed Int32 array is inertial or not.</summary>
  /// <remarks>
  /// An Int32 Array can be considered inertial if:
  /// 1: It contains at least one odd value.
  /// 2: The maximum value of the array is even.
  /// 3: Every odd value in the array is greater than every even value that is not the maximum value.</remarks>
  /// <param name="arr">Int32[] to be tested.</param>
  public static bool IsInertial(int[] arr) { 
    
    if(arr == null || arr.Length == 0)
      return false;
    
    var odds = arr.Where(i=>i%2!=0);
    var max = arr.Max();
    var evensMax = arr.Where(i=>i%2==0 &amp;&amp; i!=max);
    
    if(evensMax.Count() ==0)
      return false;
      
    var evenMax = evensMax.Max();
    
    return max%2==0 &amp;&amp; odds.Count() > 0 &amp;&amp; odds.All(v=>v>evenMax);
  }
}

答案9:

using System;
using System.Linq;

public class Kata
{
  /// <summary>
  /// Returns a Boolean representing if the passed Int32 array is inertial or not.</summary>
  /// <remarks>
  /// An Int32 Array can be considered inertial if:
  /// 1: It contains at least one odd value.
  /// 2: The maximum value of the array is even.
  /// 3: Every odd value in the array is greater than every even value that is not the maximum value.</remarks>
  /// <param name="arr">Int32[] to be tested.</param>
  public static Boolean IsInertial(Int32[] arr)
  {
    var evens = arr.Where(x => x%2 == 0).ToArray();
    var odds = arr.Where(x => x%2 != 0).ToArray();
    
    return odds.Any() &amp;&amp;
           arr.Max() % 2 == 0 &amp;&amp;
           odds.All(x => evens.Where(y => y != arr.Max()).All(y => y < x));
  }
}

答案10:

using System;
using System.Linq;

public class Kata
{
    /// <summary>
    /// Returns a Boolean representing if the passed Int32 array is inertial or not.</summary>
    /// <remarks>
    /// An Int32 Array can be considered inertial if:
    /// 1: It contains at least one odd value.
    /// 2: The maximum value of the array is even.
    /// 3: Every odd value in the array is greater than every even value that is not the maximum value.</remarks>
    /// <param name="arr">Int32[] to be tested.</param>
    public static Boolean IsInertial(Int32[] arr)
    {
        bool atLeastOneOdd = false;

        int max = arr.Max();

        int minOdd = int.MaxValue;
        int maxEven = int.MinValue;
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i] % 2 != 0)
            {
                atLeastOneOdd = true;
                if (arr[i] < minOdd) minOdd = arr[i];
            }
            else if(arr[i] > maxEven &amp;&amp; arr[i] != max)
            {
                maxEven = arr[i];
            }
        }

        return atLeastOneOdd &amp;&amp; max % 2 == 0 &amp;&amp; minOdd > maxEven;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值