using System.Linq;publicclassKata{publicstaticboolIsInertial(int[] arr){var max = arr.Max();if(max%2!=0)returnfalse;if(!arr.Any(x => x%2!=0))returnfalse;if(arr.Where(x => x%2!=0).Any(x => arr.Where(y => y%2==0).Any(y => y != max && y > x)))returnfalse;returntrue;}}
答案3:
using System;using System.Linq;publicclassKata{publicstaticBooleanIsInertial(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());}
using System;using System.Collections.Generic;using System.Linq;publicclassKata{publicstaticBooleanIsInertial(Int32[] arr){var flag =false;for(int i =0; i < arr.Length; i++){if(arr[i]%2!=0){
flag =true;break;}}if(!flag)returnfalse;if(arr.Max()%2!=0)returnfalse;
List<int> evenList =newList<int>();
List<int> oddList =newList<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)returnfalse;}}returntrue;}}
答案6:
using System;using System.Linq;publicclassKata{publicstaticBooleanIsInertial( Int32[] arr ){if( arr.All( n => n%2==0)){returnfalse;}var max = arr.Max();if( max%2!=0){returnfalse;}var even = arr.Where( n => n%2==0&& n != max ).ToArray();var odd = arr.Where( n => n%2!=0).ToArray();return odd.All( n =>!even.Any( e => e >= n ));}}
using System.Linq;publicclassKata{/// <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>publicstaticboolIsInertial(int[] arr){if(arr ==null|| arr.Length ==0)returnfalse;var odds = arr.Where(i=>i%2!=0);var max = arr.Max();var evensMax = arr.Where(i=>i%2==0&& i!=max);if(evensMax.Count()==0)returnfalse;var evenMax = evensMax.Max();return max%2==0&& odds.Count()>0&& odds.All(v=>v>evenMax);}}
答案9:
using System;using System.Linq;publicclassKata{/// <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>publicstaticBooleanIsInertial(Int32[] arr){var evens = arr.Where(x => x%2==0).ToArray();var odds = arr.Where(x => x%2!=0).ToArray();return odds.Any()&&
arr.Max()%2==0&&
odds.All(x => evens.Where(y => y != arr.Max()).All(y => y < x));}}
答案10:
using System;using System.Linq;publicclassKata{/// <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>publicstaticBooleanIsInertial(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];}elseif(arr[i]> maxEven && arr[i]!= max){
maxEven = arr[i];}}return atLeastOneOdd && max %2==0&& minOdd > maxEven;}}
惯性阵【难度:1级】:答案1:using System;using System.Linq;public class Kata{ public static bool IsInertial(int[] arr) { var inertialTester = new InertialTester(arr); return inertialTester.IsInerti...