Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace algorithms
- {
- class Program
- {
- private static int[] numbers;
- private static int totalHalf;
- private static int[,] matrix;
- private static bool[,] usedMatrix;
- public static void Main(string[] args)
- {
- numbers = Console.ReadLine().Split().Select(int.Parse).ToArray();
- int total = numbers.Sum();
- totalHalf = (int)Math.Floor((decimal)total / 2);
- int rows = numbers.Length;
- int cols = totalHalf;
- matrix = new int[rows + 1,cols + 1];
- usedMatrix = new bool[rows, cols];
- for (int i = 1; i < rows + 1; i++)
- {
- for (int j = 1; j < cols + 1; j++)
- {
- int include = -1;
- if (numbers[i - 1] <= j)
- {
- include = numbers[i - 1] + matrix[i - 1,j - numbers[i - 1]];
- }
- int exclude = matrix[i - 1,j];
- if (include > exclude)
- {
- //include the number
- matrix[i, j] = include;
- usedMatrix[i - 1, j - 1] = true;
- }
- else
- {
- //exclude the number
- matrix[i,j] = exclude;
- }
- }
- }
- List<int> result = new List<int>();
- int currentRow = rows - 1;
- int currentCol = cols - 1;
- while (currentRow >= 0 && currentCol >= 0)
- {
- if (usedMatrix[currentRow, currentCol])
- {
- result.Add(numbers[currentRow]);
- currentCol -= numbers[currentRow];
- }
- currentRow--;
- }
- int alanSum = result.Sum();
- int bobSum = Math.Abs(total - alanSum);
- Console.WriteLine($"Difference: {Math.Abs(alanSum - bobSum)}");
- Console.WriteLine($"Alan:{alanSum} Bob:{bobSum}");
- Console.WriteLine($"Alan takes: {string.Join(" ",result)}");
- Console.WriteLine($"Bob takes the rest.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement