[简单]代码片段_电梯数字排列

       今天看电梯数字排列是9 18 26这样呈倒N排列的,写了个类似的打印例子,如下:
      

import java.util.Arrays;

public class 电梯数字排列_S3_Test {
	public static void main(String[] args) {
		电梯数字排列_S3_Test t = new 电梯数字排列_S3_Test();
		int totalFloorNum=13;
		int colNum=5;
		t.getLiftNum(totalFloorNum, colNum);
		System.out.println();
		t.getLiftNumSeq(totalFloorNum, colNum);
		
	}
	
	/**
	 * @param totalFool  :楼层数
	 * @param colNum :每列数量
	 */
	public void getLiftNumSeq(int totalFloorNum, int colNum) {
		int colSum = (totalFloorNum - 1) / colNum + 1;
		int[][] liftArr = new int[colNum][colSum];
		for (int i = 0; i < colSum - 1; i++) {
			for (int j = 0; j < colNum; j++) {
				liftArr[j][i] = i * colNum + j + 1;
			}
		}
		// 最后一列
		for (int k = 0, klen = totalFloorNum - (colSum - 1) * colNum; k < klen; k++) {
			liftArr[k][colSum - 1] = (colSum - 1) * colNum + k + 1;
		}
		for (int[] is : liftArr) {
			System.out.println(Arrays.toString(is));
		}
	}

	/**
	 * @param totalFool  :楼层数
	 * @param colNum :每列数量
	 */
	public void getLiftNum(int totalFloorNum, int colNum) {
		int colSum = (totalFloorNum - 1) / colNum + 1;
		int[][] liftArr = new int[colNum][colSum];
		for (int i = 0; i < colSum - 1; i++) {
			for (int j = 0; j < colNum; j++) {
				liftArr[j][i] = (colNum - j) * (i + 1) + i * j;
			}
		}
		// 最后一列
		for (int k = 0, klen = totalFloorNum - (colSum - 1) * colNum; k < klen; k++) {
			liftArr[k][colSum - 1] = totalFloorNum - k;
		}
		for (int[] is : liftArr) {
			System.out.println(Arrays.toString(is));
		}
	}
}

 

你可能感兴趣的:(代码)