新生——S型分班算法

package org.zttc.service;

public class DistributeClass {
	
	public static final Boolean order =true;
	
	public static void main(String[] args) {
		
		//把分班后数据存入二位数组
		int test[][] = divideByClass(64,8,order);
		String str = "顺序";
		if(!order){
			str = "倒序";
		}	
		
		for(int i=0;i<8;i++){
			for(int j=0;j<test[i].length;j++){
				int x = test[i][j];
				if(x != 0){
					System.out.println("进行"+str+"排列的结果:第"+(i+1)+"班被分配的第"+(j+1)+"位学生总体排名为:"+x+"");
				}
			}
		}
		
	}

	public static int[][] divideByClass(int stuNum,int classNum,boolean type){

		//定义一个二维数组,初始化容量为 stuNum=64 classNum=8 ,int[8][9]
		int[][] rs = new int[classNum][stuNum/classNum+1];
				
		for(int i=0;i<stuNum;i++){//i<64
			
			int x = i%classNum;//求余 64/8  0 1 2 3 4 5 6 7   横坐标
			int y = i/classNum;//整除 64/8  0 1 2 3 4 5 6 7 8  纵坐标
			
			//求余x=0,整除y!=0的时候,type取反
			if(x==0 && y!=0){  // 8-false 16-true 24-false 32-true 40-false 48-true 56-true 64-false
				type=!type;
			}
					
			if(!type){
				x = classNum-i%classNum-1; //班级数-序号%班级数-1
				y = i/classNum;//序号/班级数
			}
			rs[x][y]=i+1; 
			
			/** x求余  y整除  rs数组
			 *  i=0;x=0;y=0;type=true,rs[0][0]=1;
			 *  i=1;x=1;y=0;type=true,rs[1][0]=2;
			 *  i=2;x=2;y=0;type=true,rs[2][0]=3;
			 *  i=3;x=3;y=0;type=true,rs[3][0]=4;
			 *  i=4;x=4;y=0;type=true,rs[4][0]=5;
			 *  i=5;x=5;y=0;type=true,rs[5][0]=6;
			 *  i=6;x=6;y=0;type=true,rs[6][0]=7;
			 *  i=7;x=7;y=0;type=true,rs[7][0]=8;
			 *  i=8;x=6;y=1;type=false,rs[6][1]=9;
			 *  i=9;x=6;y=1;type=false,rs[7][1]=10;
			 *  i=10;x=
			 */
		}
		return rs;
	}
}

 

posted @ 2016-10-09 09:50  ZeroWM  阅读(3744)  评论(0编辑  收藏  举报