问题描述
- 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("英语课") && 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();
}
}
}
}
解决方案
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 = { "体育课", "体育课", "美术课", "音乐课", "劳动课", "语文课", "数学课", "英语课" };
string[] xinqi = { "一", "二", "三", "四", "五" };
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, i) => new string[] { "星期" + xinqi[i] }.Concat(x.OrderBy(y => y == "自修课" ? 1 : 0).ThenBy(_ => Guid.NewGuid()).GroupBy(y => y).SelectMany(y => y)).ToArray()).ToArray();
for (int j = 0; j < 7; j++)
{
for (int i = 0; i < 5; i++)
{
Console.Write(result[i][j] + "t");
}
Console.WriteLine();
}
}
}
}
解决方案二:
星期一 星期二 星期三 星期四 星期五
体育课 英语课 音乐课 数学课 语文课
语文课 英语课 语文课 数学课 美术课
劳动课 语文课 体育课 语文课 英语课
英语课 数学课 英语课 语文课 数学课
数学课 自修课 数学课 英语课 自修课
自修课 自修课 自修课 自修课 自修课
Press any key to continue . . .