C# “贝格尔”编排法

采用“贝格尔”编排法,编排时如果参赛队为双数时,把参赛队数分一半(参赛队为单数时,最后以“0”表示形成双数),前一半由1号开始,自上而下写在左边;后一半的数自下而上写在右边,然后用横线把相对的号数连接起来。这即是第一轮的比赛。
第二轮将第一轮右上角的编号(“0”或最大的一个代号数)移到左角上,三轮又移到右角上,以此类推。
即单数轮次时“0”或最大的一个代号在右上角,双数轮次时则在左上角。如下表示:
7个队比赛的编排方法
第一轮    第二轮   第三轮   第四轮    第五轮   第六轮    第七轮
1-0  0-5  2-0  0-6  3-0  0-7  4-0
2-7  6-4  3-1  7-5  4-2  1-6  5-3
3-6  7-3  4-7  1-4  5-1  2-5  6-2
4-5  1-2  5-6  2-3  6-7  3-4  7-1
无论比赛队是单数还是双数,最后一轮时,必定是“0”或最大的一个代号在右上角,“1”在右下角。
根据参赛队的个数不同,“1”朝逆时针方向移动一个位置时,应按规定的间隔数移动(见表),“0”或最大代号数应先于“1”移动位置。
C#实现:
protected void Page_Load(object sender, EventArgs e)
{
    List<int[]> list = new List<int[]>();
    List<int> teams = new List<int> { 1, 2, 3, 4, 5, 6, 7 };
    int[] array = teams.ToArray();            
    //参赛队数数量
    int initlen = array.Length;
    //比赛轮次
    int turns = initlen - 1;
    //如果为奇数,用0补空
    if (Convert.ToBoolean(initlen % 2))
    {
        teams.Add(0);
        turns = initlen;
    }
    list.Add(teams.ToArray());
    int max = teams[teams.Count - 1];
    //间隔数,计算公式为(n-4)/2+1
    int steps = initlen <= 4 ? 1 : (initlen - 4) / 2 + 1;

    List<int> parseList = teams;
    int temp = 0;
    for (int n = 0; n < turns; n++)
    {
        //移除空位
        bool isMax = parseList[0] == max ? true : false;
        parseList.RemoveAt(parseList[0] == max ? 0 : parseList.Count - 1);
        int[] tempArray = parseList.ToArray();
        int templen = tempArray.Length;
        int tempLen = isMax ? steps + 2 : steps;
        for (int i = 0; i < tempLen; i++)
        {
            //右位移
            temp = tempArray[templen - 1];
            for (int j = templen - 2; j >= 0; j--)
            {
                tempArray[j + 1] = tempArray[j];
            }
            tempArray[0] = temp;
        }
        //补空位
        string tempString = isMax ?
            string.Format("{0},{1}", string.Join(",", tempArray), max) :
            string.Format("{0},{1}", max, string.Join(",", tempArray));
        int[] parseArray = Array.ConvertAll<string, int>(tempString.Split(','), s => int.Parse(s));
        parseList = new List<int>(parseArray);
        list.Add(parseArray);                
    }
    //分队
    for (int i = 0; i < list.Count; i++)
    {
        Response.Write(string.Format("---------第{0}轮--------<br/>", i));
        int[] ar = list[i];
        int length = ar.Length / 2;
        int[] left = new int[length], right = new int[length];
        List<int> lll = new List<int>();
        for (int j = 0; j < length; j++)
        {
            left[j] = ar[j];
            right[j] = ar[j + length];
        }
        Array.Reverse(right);
        for (int j = 0; j < left.Length; j++)
        {
            Response.Write(string.Format("{0},{1}<br/>", left[j], right[j]));
        }
    }
}

  

结果:

---------第0轮--------
1,0
2,7
3,6
4,5
---------第1轮--------
0,5
6,4
7,3
1,2
---------第2轮--------
2,0
3,1
4,7
5,6
---------第3轮--------
0,6
7,5
1,4
2,3
---------第4轮--------
3,0
4,2
5,1
6,7
---------第5轮--------
0,7
1,6
2,5
3,4
---------第6轮--------
4,0
5,3
6,2
7,1
---------第7轮--------
0,1
2,7
3,6
4,5

原文地址:https://www.cnblogs.com/sntetwt/p/8395178.html