Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace Vote
- {
- class Program
- {
- static void Main(string[] args)
- {
- int blockCount = 0;
- try
- {
- string[] lines = File.ReadAllLines(@"in.txt");
- File.Delete(@"out.txt");
- blockCount = Convert.ToInt32(lines[0]);
- int StartBlock = 0;
- int EndBlock = 0;
- for (int i = 0; i < blockCount; i++)
- {
- StartBlock = EndBlock + 2;
- int namesCount = Convert.ToInt32(lines[StartBlock]);
- string[] names = new string[namesCount];
- for (int j = 0; j < namesCount; j++)
- {
- names[j] = lines[StartBlock + j + 1];
- }
- int[][] bulletins = new int[0][];
- for (int j = 0; StartBlock + namesCount + j + 1 < lines.Length && lines[StartBlock + namesCount + j + 1] != ""; j++)
- {
- Array.Resize(ref bulletins, bulletins.Length + 1);
- string[] splitNumbers = lines[StartBlock + namesCount + j + 1].Split(' ');
- List<int> numbers = new List<int>();
- foreach (var number in splitNumbers)
- {
- numbers.Add(Convert.ToInt32(number));
- }
- bulletins[j] = numbers.ToArray<int>();
- EndBlock = StartBlock + namesCount + j + 1;
- }
- if (namesCount > 20)
- {
- Console.WriteLine(">20");
- goto for1;
- }
- for (int j = 0; j < namesCount; j++)
- {
- if (names[j].Length > 80)
- {
- Console.WriteLine(">80");
- goto for1;
- }
- }
- if (bulletins.Length > 1000)
- {
- Console.WriteLine(">1000");
- goto for1;
- }
- Console.WriteLine("Початок блоку {0}", StartBlock);
- File.AppendAllText(@"out.txt", "Блок "+(i+1)+"\n");
- foreach (var item in names)
- {
- Console.WriteLine(item);
- }
- Console.WriteLine(bulletins.Length + " бюлетнiв");
- Console.WriteLine("Кiнець блоку {0}", EndBlock);
- Console.WriteLine("");
- while (true)
- {
- Dictionary<int, int> counter = new Dictionary<int, int>();
- for (int j = 0; j < names.Length; j++)
- {
- if(names[j] != "-1") counter[j + 1] = 0;
- }
- for (int j = 0; j < bulletins.Length; j++)
- {
- counter[bulletins[j][0]] = counter[bulletins[j][0]] + 1;
- }
- int winer = -1;
- foreach (var item in counter)
- {
- if ( item.Value * 100 / bulletins.Length > 50)
- {
- winer = item.Key-1;
- break;
- }
- }
- if (winer != -1)
- {
- Console.WriteLine("Виграв {0}", names[winer]);
- File.AppendAllText(@"out.txt", "Виграв " + names[winer] + "\n");
- Console.WriteLine("");
- break;
- }
- var check_winer = counter.ToList();
- check_winer.Sort((pair1, pair2) => pair1.Value.CompareTo(pair2.Value));
- if (check_winer[0].Value == check_winer[check_winer.Count - 1].Value)
- {
- foreach (var item in counter)
- {
- Console.WriteLine("Виграв: " + names[item.Key -1]);
- File.AppendAllText(@"out.txt", "Виграв: " + names[item.Key - 1] + "\n");
- }
- Console.WriteLine("");
- break;
- }
- deleteLosers(ref bulletins, ref names, counter);
- }
- for1: continue;
- }
- File.AppendAllText(@"out.txt", "");
- }
- catch (Exception e)
- {
- Console.WriteLine("Exception: {0}", e.Message);
- }
- }
- static public int deleteLosers(ref int[][] bulleteins, ref string[] names, Dictionary<int, int> counter)
- {
- int minCount = counter.Aggregate((l, r) => l.Value < r.Value ? l : r).Value;
- while (true)
- {
- Dictionary<int, int> minPairs = counter.Where(x => x.Value == minCount).ToDictionary(i => i.Key, i => i.Value);
- foreach (var item in minPairs)
- {
- for (int i = 0; i < bulleteins.Length; i++)
- {
- bulleteins[i] = (from x in bulleteins[i]
- where x != item.Key
- select x).ToArray();
- }
- names[item.Key - 1] = "-1";
- }
- break;
- }
- return 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement