zjbonrace 2015-01-19 12:51 采纳率: 100%
浏览 1565
已采纳

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(_ => Guid.NewGuid()).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条回答 默认 最新

  • threenewbee 2015-01-19 12:56
    关注

    result = result.Select(x => x.OrderBy(_ => Guid.NewGuid()).ToArray()).ToArray();
    ->
    result = result.Select(x => x.OrderBy(y => y == "自修课" ? 1 : 0).ThenBy(_ => Guid.NewGuid()).ToArray()).ToArray();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改