Advertisement
KAMEN1973

DividingPresents

Dec 15th, 2020
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace DividingPresents
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int[] input = Console.ReadLine()
  13.                 .Split(" ")
  14.                 .Select(int.Parse)
  15.                 .ToArray();
  16.  
  17.             int[] presents = input                
  18.                 .OrderBy(e => e)
  19.                 .ToArray();
  20.  
  21.             List<int> firstHalfe = new List<int>();
  22.             List<int> secondHalfe = new List<int>();
  23.             int firstSum = 0;
  24.             int secondSum = 0;
  25.  
  26.             for (int present = presents.Length - 1; present >= 0; present--)
  27.             {
  28.                 if (firstSum <= secondSum)
  29.                 {
  30.                     firstSum += presents[present];
  31.                     firstHalfe.Add(presents[present]);
  32.                 }
  33.                 else
  34.                 {
  35.                     secondSum += presents[present];
  36.                     secondHalfe.Add(presents[present]);
  37.                 }
  38.             }
  39.  
  40.             int alan = 0;
  41.             int bob = 0;
  42.             List<int> alans;
  43.             List<int> bobs;
  44.  
  45.             if (firstSum <= secondSum)
  46.             {
  47.                 alan = firstSum;
  48.                 bob = secondSum;
  49.  
  50.                 alans = RestoreOrder(firstHalfe, input);
  51.                 bobs = secondHalfe;
  52.             }
  53.             else
  54.             {
  55.                 alan = secondSum;
  56.                 bob = firstSum;
  57.  
  58.                 alans = RestoreOrder(secondHalfe, input);
  59.                 bobs = firstHalfe;
  60.             }
  61.  
  62.             StringBuilder stringBuilder = new StringBuilder();
  63.  
  64.             stringBuilder.AppendLine($"Difference: {Math.Abs(firstSum - secondSum)}");
  65.             stringBuilder.AppendLine($"Alan:{alan} Bob:{bob}");
  66.             stringBuilder.AppendLine($"Alan takes: {string.Join(" ", alans)}");
  67.             stringBuilder.AppendLine("Bob takes the rest.");
  68.  
  69.             Console.WriteLine(stringBuilder.ToString().TrimEnd());
  70.         }
  71.  
  72.         private static List<int> RestoreOrder(List<int> sorted, int[] input)
  73.         {
  74.             List<int> originalOrder = new List<int>();
  75.  
  76.             foreach (int item in input)
  77.             {
  78.                 if (sorted.Contains(item))
  79.                 {
  80.                     originalOrder.Add(item);
  81.                     sorted.Remove(item);
  82.                 }
  83.             }
  84.  
  85.             originalOrder.Reverse();
  86.  
  87.             return originalOrder;
  88.         }
  89.     }
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement