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();
    }
}