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

产品最大【难度:1级】:

答案1:

using System.Linq;

public class Kata
{
    public static int AdjacentElementsProduct(int[] array)
    {
        return array.Skip(1).Select((x,i) => x * array[i]).Max();
    }
}

答案2:

using System.Linq;
using System;
public class Kata
{
    public static int AdjacentElementsProduct(int[] a)
      => Enumerable.Range(0,a.Length-1).Aggregate(int.MinValue, (s,n)=> Math.Max(s, a[n]*a[n+1]));
}

答案3:

using System.Linq;

public class Kata
{
    public static int AdjacentElementsProduct(int[] array)
        => Enumerable.Range(0, array.Length-1).Select(x => array[x] * array[x+1]).Max();
}

答案4:

using System;
using System.Collections.Generic;
using System.Linq;
public class Kata
{
    public static int AdjacentElementsProduct(int[] array)
    {
        return Enumerable.Range(1, array.Length - 1).Select(a => array[a - 1] * array[a]).ToArray().Max();
    }
}

答案5:


using System.Linq;


public class Kata
{
    public static int AdjacentElementsProduct(int[] array)
    {
        int max = array[0]*array[1];
        for (int i = 1; i < array.Count() - 1; i++)
                if (max < array[i] * array[i + 1])
                    max = array[i] * array[i + 1];
        return max;
    }
}

答案6:

using System;

public class Kata
{
    public static int AdjacentElementsProduct(int[] array)
    {
        int max = Int32.MinValue;
        for (int i = 0, j = 1; j < array.Length; i++, j++)
        {
            max = Math.Max(array[i] * array[j], max);
        }
        return max;
    }
}

答案7:

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

public class Kata
{
    public static int AdjacentElementsProduct(int[] array)
    {
      List<int> newArray = new List<int>();
      
      for (int i = 0; i < array.Length - 1; i++) {
        newArray.Add(array[i] * array[i + 1]);
      }
      
      return newArray.Max();
    }
}

答案8:

using System.Collections.Generic;
using System.Linq;
public class Kata
{
    public static int AdjacentElementsProduct(int[] array)
    {
        List<int> plist = new List<int>();      
        
        for(int i=0; i<array.Length-1; i++)
        {
          int product= array[i] * array[i+1];
          
          plist.Add(product);
        }
        
        return plist.Max();
    }
}

答案9:

using System.Linq;

public class Kata
{
  public static int AdjacentElementsProduct(int[] array) =>
    array.Zip(array.Skip(1), (a, b) => a * b).Max();
}

答案10:

using System.Linq;
public class Kata
{
    public static int AdjacentElementsProduct(int[] a)
    {
        return a.Select((x, y) => y < a.Length - 1 ? a[y] * a[y + 1] : int.MinValue).Max();
    }
}



你可能感兴趣的:(C#编程高级练习)