Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace Vote
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int blockCount = 0;
  15.             try
  16.             {
  17.                 string[] lines = File.ReadAllLines(@"in.txt");
  18.                 File.Delete(@"out.txt");
  19.                 blockCount = Convert.ToInt32(lines[0]);
  20.                 int StartBlock = 0;
  21.                 int EndBlock = 0;
  22.                 for (int i = 0; i < blockCount; i++)
  23.                 {
  24.                     StartBlock = EndBlock + 2;
  25.  
  26.                     int namesCount = Convert.ToInt32(lines[StartBlock]);
  27.                     string[] names = new string[namesCount];
  28.                     for (int j = 0; j < namesCount; j++)
  29.                     {
  30.                         names[j] = lines[StartBlock + j + 1];
  31.                     }
  32.                     int[][] bulletins = new int[0][];
  33.                     for (int j = 0; StartBlock + namesCount + j + 1 < lines.Length && lines[StartBlock + namesCount + j + 1] != ""; j++)
  34.                     {
  35.                         Array.Resize(ref bulletins, bulletins.Length + 1);
  36.  
  37.                         string[] splitNumbers = lines[StartBlock + namesCount + j + 1].Split(' ');
  38.                         List<int> numbers = new List<int>();
  39.                         foreach (var number in splitNumbers)
  40.                         {
  41.                             numbers.Add(Convert.ToInt32(number));
  42.                         }
  43.                         bulletins[j] = numbers.ToArray<int>();
  44.                         EndBlock = StartBlock + namesCount + j + 1;
  45.                     }
  46.                     if (namesCount > 20)
  47.                     {
  48.                         Console.WriteLine(">20");
  49.                         goto for1;
  50.                     }
  51.                     for (int j = 0; j < namesCount; j++)
  52.                     {
  53.                         if (names[j].Length > 80)
  54.                         {
  55.                             Console.WriteLine(">80");
  56.                             goto for1;
  57.                         }
  58.                     }
  59.                     if (bulletins.Length > 1000)
  60.                     {
  61.                         Console.WriteLine(">1000");
  62.                         goto for1;
  63.                     }
  64.                     Console.WriteLine("Початок блоку {0}", StartBlock);
  65.                     File.AppendAllText(@"out.txt", "Блок "+(i+1)+"\n");
  66.                     foreach (var item in names)
  67.                     {
  68.                         Console.WriteLine(item);
  69.                     }
  70.                     Console.WriteLine(bulletins.Length + " бюлетнiв");
  71.                     Console.WriteLine("Кiнець блоку {0}", EndBlock);
  72.                     Console.WriteLine("");
  73.                     while (true)
  74.                     {
  75.                         Dictionary<int, int> counter = new Dictionary<int, int>();
  76.                         for (int j = 0; j < names.Length; j++)
  77.                         {
  78.                             if(names[j] != "-1") counter[j + 1] = 0;
  79.                         }
  80.                         for (int j = 0; j < bulletins.Length; j++)
  81.                         {
  82.                             counter[bulletins[j][0]] =  counter[bulletins[j][0]] + 1;
  83.                         }
  84.                         int winer = -1;
  85.                         foreach (var item in counter)
  86.                         {
  87.                             if ( item.Value * 100 / bulletins.Length > 50)
  88.                             {
  89.                                 winer = item.Key-1;
  90.                                 break;
  91.                             }
  92.                         }
  93.                         if (winer != -1)
  94.                         {
  95.                             Console.WriteLine("Виграв {0}", names[winer]);
  96.                             File.AppendAllText(@"out.txt", "Виграв " + names[winer] + "\n");
  97.                             Console.WriteLine("");
  98.                             break;
  99.                         }
  100.                         var check_winer = counter.ToList();
  101.                         check_winer.Sort((pair1, pair2) => pair1.Value.CompareTo(pair2.Value));
  102.                         if (check_winer[0].Value == check_winer[check_winer.Count - 1].Value)
  103.                         {
  104.                             foreach (var item in counter)
  105.                             {
  106.                                 Console.WriteLine("Виграв: " + names[item.Key -1]);
  107.                                 File.AppendAllText(@"out.txt", "Виграв: " + names[item.Key - 1] + "\n");
  108.                             }
  109.                             Console.WriteLine("");
  110.                             break;
  111.                         }
  112.                         deleteLosers(ref bulletins, ref names, counter);
  113.                     }
  114.                     for1: continue;
  115.                 }
  116.                 File.AppendAllText(@"out.txt", "");
  117.             }
  118.             catch (Exception e)
  119.             {
  120.                 Console.WriteLine("Exception: {0}", e.Message);
  121.             }
  122.         }
  123.  
  124.         static public int deleteLosers(ref int[][] bulleteins, ref string[] names, Dictionary<int, int> counter)
  125.         {
  126.  
  127.             int minCount = counter.Aggregate((l, r) => l.Value < r.Value ? l : r).Value;
  128.             while (true)
  129.             {
  130.                 Dictionary<int, int> minPairs = counter.Where(x => x.Value == minCount).ToDictionary(i => i.Key, i => i.Value);
  131.                 foreach (var item in minPairs)
  132.                 {
  133.                     for (int i = 0; i < bulleteins.Length; i++)
  134.                     {
  135.                         bulleteins[i] = (from x in bulleteins[i]
  136.                                          where x != item.Key
  137.                                          select x).ToArray();
  138.                     }
  139.                     names[item.Key - 1] = "-1";
  140.                 }
  141.                 break;
  142.             }
  143.  
  144.  
  145.             return 1;
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement