新生——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;
	}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM