随机分配座位,共50个学生,使学号相邻的同学座位不能相邻
编辑: 面试啦 发布时间: 2023-07-29 15:39:45
随机分配座位,共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();
}
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();
}
词条:c面试题
面试题库导航
- C++面试题
- JAVA面试题
- Oracle面试题
- MySQL面试题
- Linux面试题
- 网络工程师面试题
- 网管面试题
- 软件工程师面试题
- PHP面试题
- .net面试题
- 软件测试面试题
- EJB面试题
- Spring面试题
- Javascript面试题
- Python面试题
- QTP面试题
- Servlet面试题
- J2EE面试题
- Database面试题
- ASP面试题
- Unix面试题
- Loadrunner面试题
- 通信面试题
- 嵌入式面试题
- Ruby面试题
- Delphi面试题
- Mobile开发面试题
- SOA面试题
- JAVA面试题面试题
- 研发工程师面试题
- 软件架构师面试题
- 系统工程师面试题
- Android面试题
- JAVA程序员
- C#面试题