随机分配座位,共50个学生,使学号相邻的同学座位不能相邻(早些时候用C#写的,没有用C改写)。

admin2019-03-29  93

问题 随机分配座位,共50个学生,使学号相邻的同学座位不能相邻(早些时候用C#写的,没有用C改写)。

选项

答案static void Main(string[] args) { int Tmp = 0, Count = 50; int[] Seats = new int[Count]; bool[] Students = new bool[Count]; System.Random RandStudent=new System.Random(); Students[Seats[0]=RandStudent.Next(0,Count)]=true; for(int i = 1; i < Count; ) { Tmp=(int)RandStudent.Next(0,Count); if((!Students[Tmp])&&(Seats[i-1]-Tmp!=1) && (Seats[i-1] - Tmp) != -1){ Seats[i++] = Tmp; Students[Tmp] = true; } } foreach(int Student in Seats) System.Console.Write(Student + " "); System.Console.Read(); }

解析
转载请注明原文地址:https://jikaoti.com/ti/lag7FFFM
0

最新回复(0)