【问题标题】:I need to make groups of 4-4-3 out of this shuffledClassroom list我需要从这个 shuffledClassroom 列表中组成 4-4-3 组
【发布时间】:2016-07-30 19:06:33
【问题描述】:
static void Main(string[] args)
{
    List<string> classroom = new List<string> { "Armen", "Babken", "Hayk", "Edgar", "Tatev", "Anna", "Aram", "Karo", "Baghdig", "Harut", "Ruzanna" };
    foreach (string student in classroom)
    {
        Console.WriteLine(student);
    }

    List<string> shuffledClassroom = ShuffleList(classroom);
    foreach (string student in shuffledClassroom)
    {
        Console.WriteLine(student);
    }

    Console.ReadLine();
}

static List<T> ShuffleList<T>(List<T> inputList)
{
    List<T> randomList = new List<T>();

    Random rnd = new Random();
    int randomIndex = 0;
    while (inputList.Count > 0)
    {
        randomIndex = rnd.Next(0, inputList.Count); 
        randomList.Add(inputList[randomIndex]);
        inputList.RemoveAt(randomIndex);
    }

    return randomList;
}

【问题讨论】:

    标签: c# string random grouping tstringlist


    【解决方案1】:

    试试这个

    List<string> classroom = new List<string> { "Armen", "Babken", "Hayk", "Edgar", "Tatev", "Anna", "Aram", "Karo", "Baghdig", "Harut", "Ruzanna" };
    var groups = classroom.Select((x,i) => new {name = x, group = (int)i/4}).GroupBy(y => y.group).Select(z => z.Select(a => a.name).ToArray()).ToArray();
    

    获取随机列表

    List<string> classroom = new List<string> { "Armen", "Babken", "Hayk", "Edgar", "Tatev", "Anna", "Aram", "Karo", "Baghdig", "Harut", "Ruzanna" };
    Random rand = new Random();
    var groups = classroom.Select(m => new { name = m, rand = rand.Next()}).OrderBy(x => x.rand).Select((x,i) => new {name = x.name, group = (int)i/4}).GroupBy(y => y.group).Select(z => z.Select(a => a.name).ToArray()).ToArray();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      • 2017-06-23
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多