我刚刚看到你的最后一个问题没有答案就关闭了。
我已经在 C# 中实现了解决此问题的实用方法,假设希望在以下格式的 csv 文件中给出:
2,10,6
2,7,3
4,7,1
...
列的数量,即偏好的数量,对我的实现无关紧要。
显然,性能还有改进的空间,但为了回答,我选择让它更具可读性。
下面是代码中用到的类:
public class Student
{
public int StudentNumber { get; set; }
public List<WishVote> WishVotes { get; set; }
}
public class WishVote
{
public int WishNumber { get; set; }
public int Order { get; set; }
public string Id { get; set; }
}
public class WishVoteResult
{
public int WishNumber { get; set; }
// wish order
// vote count for that order
// wish number
public List<Tuple<int, int, int>> Assignments { get; set; }
public int TotalVoteCount { get; set; }
}
下面是您可以在 Main 中运行的代码,以按降序输出愿望的数字和总票数:
var lines = File.ReadAllLines("wishes.csv").ToList();
int studentNumber = lines.Count;
var students = new List<Student>();
int currentLine = 0;
lines.ForEach(l =>
{
currentLine++;
var wishVotes = new List<WishVote>();
int wishOrder = 0;
l.Split(',').ToList().ForEach(w =>
{
wishOrder++;
wishVotes.Add(new WishVote
{
Id = Guid.NewGuid().ToString(),
Order = wishOrder,
WishNumber = Convert.ToInt32(w)
});
});
students.Add(new Student
{
StudentNumber = currentLine,
WishVotes = wishVotes
});
});
var allWishVotes = students.SelectMany(s => s.WishVotes).ToList();
List<int> uniqueWishes = allWishVotes.Select(w => w.WishNumber).Distinct().ToList();
var wishVoteResults = new List<WishVoteResult>();
// assuming every row in the file has the same number of columns
int orderCount = students.First().WishVotes.Max(w => w.Order);
uniqueWishes.ForEach(uw =>
{
var wishVoteResult = new WishVoteResult
{
WishNumber = uw,
TotalVoteCount = allWishVotes.Where(w => w.WishNumber == uw).Count(),
Assignments = new List<Tuple<int, int, int>>()
};
for(int i = 1; i <= orderCount; i++)
{
wishVoteResult.Assignments.Add(new Tuple<int, int, int>(i, allWishVotes.Where(w => w.Order == i && w.WishNumber == uw).Count(), uw));
}
wishVoteResults.Add(wishVoteResult);
});
var assignments = wishVoteResults.SelectMany(w => w.Assignments).OrderByDescending(a => a.Item2).ThenBy(a => a.Item1).ToList();
Console.WriteLine("Wish {wishNumber}: {voteCount} {wishOrder}");
foreach (var assignment in assignments)
{
Console.WriteLine($"Wish number {assignment.Item3}: {assignment.Item2} {assignment.Item1}");
}
Console.WriteLine("Finished.");
实际上,我们所做的是创建所有可能的分配,然后进行相应的排序。