程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> new-link關於課程表的問題,我想再加一個表頭,請問怎麼做?

new-link關於課程表的問題,我想再加一個表頭,請問怎麼做?

編輯:編程綜合問答
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();
            }
        }
    }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved