Skip to content

Instantly share code, notes, and snippets.

@unilecs
Last active November 15, 2019 06:53
Show Gist options
  • Save unilecs/eba30463d8e25ca1a4c240929f401287 to your computer and use it in GitHub Desktop.
Save unilecs/eba30463d8e25ca1a4c240929f401287 to your computer and use it in GitHub Desktop.
Задача: Стоимость арифметических операций
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
private static int GetCostOfSumNumbers(int[] numbers)
{
int result = 0;
var collection = new List<int>(numbers.OrderBy(n => n));
while (collection.Count > 1)
{
int x = collection[0]; collection.Remove(x);
int y = collection[0]; collection.Remove(y);
int res = x + y;
collection.Add(res);
collection = collection.OrderBy(n => n).ToList();
result += (res);
}
return result;
}
public static void Main()
{
Console.WriteLine("UniLecs");
Console.WriteLine(string.Format("Answer = {0}", GetCostOfSumNumbers(new int[] { 1, 2, 3 }))); // 9
Console.WriteLine(string.Format("Answer = {0}", GetCostOfSumNumbers(new int[] { 1, 2, 3, 4 }))); // 19
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment