c# - 从列表创建字符串对 - 茶任何人?

标签 c# string

我们公司有 x 个人。我们想在 C# 中生成一个轮盘赌 - 茶歇应用程序来运行,订阅此功能的人会将他们的名字添加到文本文件的列表中。

然后我们想“旋转轮子”(点击一个按钮...)从这个名单中生成一对,然后他们将一起安排茶歇时间。

示例列表(从文本文件加载):

  1. 彼得
  2. 玛丽
  3. 詹姆斯
  4. 院长
  5. 劳拉

一旦我们点击旋转轮子的按钮 我们需要:

生成一个新的配对名称列表,例如:

  • 玛丽会和詹姆斯一起喝茶
  • Dean 将与 Peter 喝茶
  • 琼将与劳拉喝茶

我正在尝试弄清楚如何对此进行编码。 状况: 本月旋转的输出存储到另一个文本文件中,詹姆斯本月与玛丽喝茶,并且至少 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;
        }
    }
}

我感谢对此的任何帮助或指导。 预先感谢您的帮助。 长度

最佳答案

我是这样写的:

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);
    }
}

关于c# - 从列表创建字符串对 - 茶任何人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37587375/

相关文章:

c# - 使用 azure 表存储进行分页(升序)

c# - 预定义单词集中包含的单词的正则表达式

c# - 应如何将 IIS 托管的基于 ASP.Net 的系统部署到 Azure Service Fabric?

c - 如何提取字符串中的数据?

java - String.replace() 方法不打印 java 中的完整字符串

string - 计算给定字符串 Haskell 中 Char 的实例数

c# - 建立中央 SqlConnection 的最佳方式

c# - 如何制作 00 :00 timer in c#?

python - 条件正则表达式 : if A and B, 选择 B

string - 从大量字符串中搜索 "is in string"的高效数据结构