本文共 3040 字,大约阅读时间需要 10 分钟。
protected void Page_Load(object sender, EventArgs e){ Listlist = new List (); List teams = new List { 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 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 (tempString.Split(','), s => int.Parse(s)); parseList = new List (parseArray); list.Add(parseArray); } //分队 for (int i = 0; i < list.Count; i++) { Response.Write(string.Format("---------第{0}轮-------- ", i)); int[] ar = list[i]; int length = ar.Length / 2; int[] left = new int[length], right = new int[length]; List lll = new List (); 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} ", left[j], right[j])); } }}
结果:
---------第0轮--------
1,02,73,64,5---------第1轮--------0,56,47,31,2---------第2轮--------2,03,14,75,6---------第3轮--------0,67,51,42,3---------第4轮--------3,04,25,16,7---------第5轮--------0,71,62,53,4---------第6轮--------4,05,36,27,1---------第7轮--------0,12,73,64,5转载地址:http://vokwo.baihongyu.com/