using System;
using System.Linq;
public class Program
{
public static void Main()
{
char[,] votes = new char[25, 4] { /* your votes here */ };
var bordaResult = Borda(votes);
var condorcetResult = Condorcet(votes);
Console.WriteLine("Borda result: " + bordaResult);
Console.WriteLine("Condorcet result: " + condorcetResult);
}
public static char Borda(char[,] votes)
{
int[] score = new int[4];
for (int i = 0; i < votes.GetLength(0); i++)
{
for (int j = 0; j < votes.GetLength(1); j++)
{
score[votes[i, j] - 'a'] += votes.GetLength(1) - 1 - j;
}
}
return (char)('a' + Array.IndexOf(score, score.Max()));
}
public static char? Condorcet(char[,] votes)
{
int[,] comparison = new int[4, 4];
for (int i = 0; i < votes.GetLength(0); i++)
{
for (int j = 0; j < votes.GetLength(1); j++)
{
for (int k = j + 1; k < votes.GetLength(1); k++)
{
comparison[votes[i, j] - 'a', votes[i, k] - 'a']++;
}
}
}
for (int i = 0; i < 4; i++)
{
bool winner = true;
for (int j = 0; j < 4; j++)
{
if (i != j && comparison[i, j] <= comparison[j, i])
{
winner = false;
break;
}
}
if (winner) return (char)('a' + i);
}
return null;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
var votes = new List { "aabbccdd", "dcbadacb", "cdbabacd", "abcdabcd", "dcbaacdb" };
Console.WriteLine("Правило Борда:");
var bordaResults = CalculateBorda(votes);
foreach (var result in bordaResults)
{
Console.WriteLine("Альтернатива: " + result.Item1 + ", баллы: " + result.Item2);
}
Console.WriteLine("\nПравило Кондорсе:");
var condorcetResults = CalculateCondorcet(votes);
foreach (var result in condorcetResults)
{
Console.WriteLine("Альтернатива: " + result.Item1 + ", побед: " + result.Item2);
}
}
static List> CalculateBorda(List votes)
{
var bordaScores = new Dictionary();
foreach (var vote in votes)
{
for (int i = 0; i < vote.Length; i++)
{
if (!bordaScores.ContainsKey(vote[i]))
{
bordaScores[vote[i]] = 0;
}
bordaScores[vote[i]] += vote.Length - i - 1;
}
}
return bordaScores.OrderByDescending(x => x.Value).Select(x => new Tuple(x.Key, x.Value)).ToList();
}
static List> CalculateCondorcet(List votes)
{
var condorcetWins = new Dictionary();
var alternatives = votes.First().ToCharArray();
foreach (var alt1 in alternatives)
{
foreach (var alt2 in alternatives)
{
if (alt1 != alt2)
{
int alt1Wins = 0;
for (int i = 0; i < votes.Count; i++)
{
if (votes[i].IndexOf(alt1) < votes[i].IndexOf(alt2))
{
alt1Wins++;
}
}
if (alt1Wins > votes.Count / 2)
{
if (!condorcetWins.ContainsKey(alt1))
{
condorcetWins[alt1] = 0;
}
condorcetWins[alt1]++;
}
}
}
}
return condorcetWins.OrderByDescending(x => x.Value).Select(x => new Tuple(x.Key, x.Value)).ToList();
}
}
Сделал ИИ ссылка static T[] Shuffle(T[] _InitialSet)
{
T[] Result = new T[_InitialSet.Length];
Random r = new Random();
int n = _InitialSet.Length;
do
{
int k = r.Next(n);
Result[n - 1] = _InitialSet[k];
_InitialSet[k] = _InitialSet[n - 1];
}
while (--n > 0);
return Result;
}
static string GetRandomPreferences() => new string(Shuffle("abcd".ToCharArray()));
static char WinnerByBorda(string[] _VoterPrefs)
{
Dictionary Counts = _VoterPrefs[0].ToCharArray().Select(x => new KeyValuePair(x, 0)).ToDictionary();
foreach (var x in _VoterPrefs)
for (int i = 0; i < x.Length; i++)
Counts[x[i]] += i;
return Counts.OrderBy(x => -x.Value).First().Key;
}
static char WinnerByCondorcet(string[] _VoterPrefs)
{
Dictionary<(char, char), int> Counts = new Dictionary<(char, char), int>();
foreach (char c1 in _VoterPrefs[0].ToCharArray())
foreach (char c2 in _VoterPrefs[0].ToCharArray())
if (c1 != c2)
Counts.Add((c1, c2), 0);
foreach (var x in _VoterPrefs)
foreach (var y in Counts)
if (x.IndexOf(y.Key.Item1) < x.IndexOf(y.Key.Item2))
Counts[y.Key]++;
return Counts.OrderBy(x => x.Value).First().Key.Item1;
}
static void Main(string[] args)
{
string[] VoterPrefs = Enumerable.Range(0, 25).Select(x => GetRandomPreferences()).ToArray();
Console.WriteLine(VoterPrefs.Aggregate(string.Empty, (x, y) => x += y + Environment.NewLine));
Console.WriteLine(WinnerByBorda(VoterPrefs));
Console.WriteLine(WinnerByCondorcet(VoterPrefs));
}