-
-
Save unilecs/eba30463d8e25ca1a4c240929f401287 to your computer and use it in GitHub Desktop.
Задача: Стоимость арифметических операций
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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