随机分配座位,共50个学生,使学号相邻的同学座位不能相邻
生活随笔
收集整理的這篇文章主要介紹了
随机分配座位,共50个学生,使学号相邻的同学座位不能相邻
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1 public static void assignSeat(){
2 int count = 50;
3 int[][] seats = new int[2][count];//int[0]是50個座位,存儲值為學(xué)生號,int[1]是對應(yīng)50個學(xué)生,值為1則為已分配,0未分配
4 Random random = new Random();
5 int student = random.nextInt(count);
6 seats[0][0] = student;//先分配第一個座位給student號學(xué)生
7 seats[1][student] = 1;
8 for(int i = 1 ; i < count ;){
9 student = random.nextInt(count);
10 if(seats[1][student] != 1 &&
11 (student - seats[0][i-1]) !=1 &&
12 (student - seats[0][i-1]) !=-1){
13 seats[0][i++] = student;
14 seats[1][student] = 1;
15 }
16 System.out.println( "loop");
17 }
18
19 for(int stu : seats[0])
20 System.out.print(stu + ",");
21 }
?
轉(zhuǎn)載于:https://www.cnblogs.com/music180/p/4938489.html
總結(jié)
以上是生活随笔為你收集整理的随机分配座位,共50个学生,使学号相邻的同学座位不能相邻的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用cookie实现localstorag
- 下一篇: Fragment官方解析