在我们公司,我们有x个人。我们希望在C#中生成轮盘赌 - 茶歇应用程序,其中订阅此功能的人将其名称添加到文本文件的列表中。
然后我们想要“旋转方向盘”(点击一个按钮......),它会从这个名单中生成一对,然后他们会一起安排茶歇。
样本列表(从文本文件加载):
点击按钮旋转滚轮 我们需要:
生成一个新的配对名称列表,例如:
我想弄清楚如何编码。 条件: 这个月旋转的输出存储在另一个文本文件中,詹姆斯本月与玛丽喝茶,并且至少6个月不再与玛丽喝茶。 示例文本文件 - 每月附加:
* 01MAY2015,2,4
* 01MAY2015,5,1
* 01MAY2015,3,6
* etc...
最终输出列表项目计数将是主列表中项目数量的一半。因此,每当车轮旋转时,你就不会看到任何名字都有多人喝茶。
下个月,我们将检查前几个月的输出列表,以确保名称不会再次相互配对。
以前有人做过这样的事吗?
我的配对代码如下:
在“旋转”按钮的点击事件中:
List<string> q = new List<string>(Variables.gsNames); //I load the names into a global variable list
List<string> pairs = new List<string>(Variables.gsNames.Length * (Variables.gsNames.Length - 1)); // I load the pairs into another global var list
while (q.Count > 0)
{
string n1 = q[0];
q.RemoveAt(0); // name1 was next in line
// try to pair with the first in line who have not been paired with this name before
foreach (string n2 in q)
{
// create a normalized "candidate" pair with the next name in line
string p = string.Compare(n1, n2) < 0 ? n1 + " and " + n2 : n2 + " and " + n1;
if (!pairs.Contains(p))
{
// the two staffmembers have not been paired before
pairs.Add(p);
q.Remove(n2);
q.Add(n1);
q.Add(n2);
ListViewItem lvi = new ListViewItem(p);
lvPairs.Items.Add(lvi);
// the pair is recorded and the names have been moved to the end of the line
break;
}
}
}
我感谢任何帮助或指导。 预先感谢您的帮助。 莱恩
答案 0 :(得分:0)
我写了这样的话:
var matches = new List<TeaParty>();
var people = new List<Person>()
{
new Person { Id = 1, Name = "Peter" },
new Person { Id = 2, Name = "Mary" },
new Person { Id = 3, Name = "Joan" },
new Person { Id = 4, Name = "James" },
new Person { Id = 5, Name = "Dean" },
new Person { Id = 6, Name = "Laura" },
};
我的想法是及时与一个人合作,看看他/她是否违反了任何规定:
var startDate = new DateTime(2016, 01, 01);
foreach (var iteration in Enumerable.Range(0, 10))
{
var thisMonth = startDate.AddMonths(iteration);
foreach (var person in people)
{
var invitee = people.FirstOrDefault(potential =>
// can't have tea alone
potential.Id != person.Id &&
!matches.Any(previous =>
// can't party more than once per month
(previous.Date == thisMonth && previous.Contains(person, potential)) ||
// can't have tea with same person within 6 months
(previous.Date >= thisMonth.AddMonths(-6) &&
(
previous.Contains(person) && previous.Contains(potential)
))));
if (invitee != null)
{
var party = new TeaParty(thisMonth, person, invitee);
matches.Add(party);
Console.WriteLine(party);
}
}
Console.WriteLine("Press ENTER to process the matches for the next month");
Console.ReadLine();
}
但是,该程序在第一次交互后失败:
Month #1
Peter will have tea with Mary
Joan will have tea with James
Dean will have tea with Laura
Month #2
Peter will have tea with Joan
Mary will have tea with James
迪恩在哪儿?
当Peter和Mary在他们中间交换伙伴时,Dean再次与Laura一起喝茶,这是规则所禁止的。
如果有人想继续这一点,请遵循其余代码:
class Person
{
public int Id { get; set; }
public string Name { get; set; }
public override string ToString()
{
return this.Name;
}
}
class TeaParty
{
public DateTime Date { get; set; }
public Person Inviter { get; set; }
public Person Invitee { get; set; }
public TeaParty() { }
public TeaParty(DateTime date, Person inviter, Person invitee) : this()
{
this.Date = date;
this.Inviter = inviter;
this.Invitee = invitee;
}
public bool Contains(params Person[] guests)
{
return guests != null && guests
.Any(guest => this.Inviter.Id == guest.Id || this.Invitee.Id == guest.Id);
}
public override string ToString()
{
return String.Format("{0} will have tea with {1} at {2:D}",
this.Inviter, this.Invitee, this.Date);
}
}