Names and teams
user_1747955
csharp
a year ago
4.3 kB
6
Indexable
using System; using System.Linq; using System.Collections.Generic; Console.OutputEncoding = System.Text.Encoding.UTF8; List<string> Names = new List<string> { "Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7", "Name8", "Name9", "Name10", "Name11" }; // расчет количества команд int teamsAmount = CalculateTeamsAmount(); bool exitRequested = false; while (!exitRequested) { Console.WriteLine("Добро пожаловать в Рандомайзер!"); Console.WriteLine("0. Список участников"); Console.WriteLine("1. Добавить новое имя"); Console.WriteLine("============================"); Console.WriteLine("Выберите способ генерации: "); Console.WriteLine("2. По порядку"); Console.WriteLine("3. По случайности"); Console.WriteLine("4. Выйти"); Console.Write("Выбор: "); int inputPoint = Convert.ToInt32(Console.ReadLine()); switch (inputPoint) { case 0: ShowParticipantList(); break; case 1: AddNewName(); break; case 2: GenerateTeamsByOrder(teamsAmount); break; case 3: Console.Write("Введите количество участников в команде: "); int teamSize = Convert.ToInt32(Console.ReadLine()); GenerateTeamsByRandom(teamsAmount, teamSize); break; case 4: exitRequested = true; break; default: break; } } int CalculateTeamsAmount() { int teamsAmount = 0; if (Names.Count > 0) { teamsAmount = Names.Count / 2; if (Names.Count % 2 > 0) { teamsAmount += 1; Console.WriteLine("количество команд: " + teamsAmount); } else { Console.WriteLine("количество команд: " + teamsAmount); } } else { Console.WriteLine("No players"); } return teamsAmount; } void GenerateTeamsByOrder(int teamsAmount) { List<string>[] teams = new List<string>[teamsAmount]; int j = 0; for (int i = 0; i < teams.Length; i++, j += 2) { teams[i] = new List<string>(); if ((j + 1) < Names.Count) { teams[i].Add(Names[j]); teams[i].Add(Names[j + 1]); } else { teams[i].Add(Names[j]); } }; for (int i = 0; i < teams.Length; i++) { Console.WriteLine($"Команда {i + 1}: {string.Join(", ", teams[i])}"); } } void GenerateTeamsByRandom(int teamsAmount, int teamSize) { Random random = new Random(); // Shuffle the Names list using Fisher-Yates algorithm for (int i = Names.Count - 1; i > 0; i--) { int j = random.Next(0, i + 1); string temp = Names[i]; Names[i] = Names[j]; Names[j] = temp; } // Create teams based on the shuffled Names list List<string>[] teams = new List<string>[teamsAmount]; int currentIndex = 0; Console.WriteLine($"Количество участников в команде: {teamSize}"); for (int i = 0; i < teams.Length; i++) { teams[i] = new List<string>(); for (int j = 0; j < teamSize; j++) { if (currentIndex < Names.Count) { teams[i].Add(Names[currentIndex++]); } } if (teams[i].Count > 0) { Console.WriteLine($"Команда {i + 1}: {string.Join(", ", teams[i])}"); } } } void AddNewName() { Console.Write("Введите новое имя: "); string newName = Console.ReadLine(); Names.Add(newName); Console.WriteLine($"Имя \"{newName}\" успешно добавлено в список участников."); } void ShowParticipantList() { Console.WriteLine("Список участников:"); Console.WriteLine("==================="); foreach (var name in Names) { Console.WriteLine(name); } }
Editor is loading...
Leave a Comment