Advertisement
StreetKatya

Близнецы

Feb 20th, 2023
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Близнецы
  9. {
  10.     internal class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int n; //Количество монет
  15.             int[] money;//Массив для монет
  16.             int totalSum = 0;//Вся сумма
  17.             int currentSum = 0;//Текущая сумма
  18.             double halfSum;//Половина суммы
  19.             int count = 0;//Счётчик монет
  20.             using(var sr = new StreamReader("input.txt"))
  21.             {
  22.                 n = int.Parse(sr.ReadLine());
  23.                 string[] str = sr.ReadLine().Split(' ');
  24.                 money = new int[n];
  25.                 for (int i = 0; i < n; i++)//Заполняем массив монетами
  26.                 {
  27.                     money[i] = int.Parse(str[i]);
  28.                     totalSum += money[i];
  29.                 }
  30.                 Array.Sort(money);//Сортируем массив
  31.                 Array.Reverse(money);//Переворачиваем задом-наперед
  32.                 double temp = totalSum / 2;
  33.                 halfSum = Math.Ceiling(temp);//Находим половину суммы монет, округляя в большую сторону
  34.             }
  35.             while(currentSum <= halfSum)//Пока текущая сумма меньше или равна половине, берём монеты начиная с самых больших (т.к. массив отсортирован и перевёрнут)
  36.             {
  37.                 currentSum += money[count];
  38.                 count++;
  39.             }
  40.             Console.WriteLine(count);//Выводим ответ
  41.             Console.ReadKey();
  42.         }
  43.     }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement