随机分配座位,共50个学生,使学号相邻的同学座位不能相邻

 1     public static void assignSeat(){
 2         int count = 50;
 3         int[][] seats = new int[2][count];//int[0]是50个座位,存储值为学生号,int[1]是对应50个学生,值为1则为已分配,0未分配
 4         Random random = new Random();
 5         int student = random.nextInt(count);
 6         seats[0][0] = student;//先分配第一个座位给student号学生
 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     }

 

posted @ 2015-11-05 09:48  music180  阅读(565)  评论(0编辑  收藏  举报