zjbonrace 2015-01-19 14:30 采纳率: 100%
浏览 1678
已采纳

link关于课程表的问题要求每天都有自修课,请问在如下代码的基础上怎么修改?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] courses = { "体育课", "体育课", "美术课", "音乐课", "劳动课", "语文课", "数学课", "英语课"};
var result = Enumerable.Range(0, 5).Select(_ => Enumerable.Range(0, 6).Select(__ => "").ToArray()).ToArray();
while (!result.All(x => x.Contains("语文课") && x.Contains("数学课") && x.Contains("英语课")))
{
var rndseq = Enumerable.Range(0, 30).OrderBy(_ => Guid.NewGuid()).Zip(courses, (x, y) => new { x, y }).ToArray();
for (int i = 0; i < 5; i++)
{
int pos = 0;
string[] cs = { "语文课", "数学课", "英语课" };
for (int j = 0; j < 6; j++)
{
if (rndseq.Any(x => x.x == i * 6 + j))
result[i][j] = rndseq.First(x => x.x == i * 6 + j).y;
else
result[i][j] = (pos++ < 3) ? cs[pos - 1] : "自修课";
}
}
}
result = result.Select(x => x.OrderBy(y => y == "自修课" ? 1 : 0).ThenBy(_ => Guid.NewGuid()).GroupBy(y => y).SelectMany(y => y).ToArray()).ToArray();
for (int j = 0; j < 6; j++)
{
for (int i = 0; i < 5; i++)
{
Console.Write(result[i][j] + "\t");
}
Console.WriteLine();
}
}
}
}

  • 写回答

3条回答 默认 最新

  • threenewbee 2015-01-19 14:36
    关注
     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] courses = { "体育课", "体育课", "美术课", "音乐课", "劳动课", "语文课", "数学课", "英语课"};
                var result = Enumerable.Range(0, 5).Select(_ => Enumerable.Range(0, 6).Select(__ => "").ToArray()).ToArray();
                while (!result.All(x => x.Contains("语文课") && x.Contains("数学课") && x.Contains("英语课") && x.Contains("自修课")))
                {
                    var rndseq = Enumerable.Range(0, 30).OrderBy(_ => Guid.NewGuid()).Zip(courses, (x, y) => new { x, y }).ToArray();
                    for (int i = 0; i < 5; i++)
                    {
                        int pos = 0;
                        string[] cs = { "语文课", "数学课", "英语课" };
                        for (int j = 0; j < 6; j++)
                        {
                            if (rndseq.Any(x => x.x == i * 6 + j))
                                result[i][j] = rndseq.First(x => x.x == i * 6 + j).y;
                            else
                                result[i][j] = (pos++ < 3) ? cs[pos - 1] : "自修课";
                        }
                    }
                }
                result = result.Select(x => x.OrderBy(y => y == "自修课" ? 1 : 0).ThenBy(_ => Guid.NewGuid()).GroupBy(y => y).SelectMany(y => y).ToArray()).ToArray();
                for (int j = 0; j < 6; j++)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        Console.Write(result[i][j] + "\t");
                    }
                    Console.WriteLine();
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么