ウィジェット-三角行列のようなデータを印刷

3823 ワード

バージョン1
public class pbox {
	public static void main(String[] args) {
		printBox(9);
	}

	public static void printBox(int row) {
		if (row < 1) {
			System.out.println("......");
			return;
		} else if (row > 80) {
			System.out.println("......");
			return;
		}
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < row; j++) {
				int s = i + j;
				int sm = s > row - 1 ? s - (row - 1) : 0;
				int s1 = sumOfAS(1, s, 1);
				int s2 = sumOfAS(2, sm - 1, 2);
				int sf = s1 - s2;
				int x = s % 2 == 0 ? j - sm : i - sm;
				int oyeah = x + sf + 1;
				System.out.printf("%3d", oyeah);
			}
			System.out.println();
		}

	}

	//  
	public static int sumOfAS(int o, int n, int j) {
		return o * n + (n - 1) * n * j / 2;
	}
}

 
バージョン2
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		for (int x = 1; x < 10; x++) {
			for (int y = 1; y < 10; y++) {
				if ((x + y) <= 10) {
					int lineNum = x + y - 1;

					int topOfLast = (lineNum - 1) * (lineNum) / 2;

					// if direction is true,up
					// otherwise, down
					boolean direction = lineNum % 2 != 0 ? true : false;

					if (direction) {
						System.out.print("" + (topOfLast + y) + "  ");
					} else {
						System.out.print("" + (topOfLast + lineNum - y + 1)
								+ "  ");
					}
				} else {
					int innerX = x;
					int innerY = y;
					int offsetX = 1;
					int offsetY = 8;

					innerX -= offsetX;
					innerY -= offsetY;

					int lineNum = innerX + innerY - 1;

					int loopNum = 0;
					int start = 8;
					for (int j = 1; j < lineNum; j++) {
						loopNum += start;
						start--;
					}
					int topOfLast = loopNum + 45;
					
					// if direction is true,up
					// otherwise, down
					boolean direction = lineNum % 2 == 0 ? true : false;

					if (direction) {
						System.out.print("" + (topOfLast + 10 - x)
								+ "  ");						
					} else {

						System.out.print("" + (topOfLast + x - lineNum ) + "  ");
					}
				}
			}
			System.out.println();
		}

	}

}

バージョン3
public class PrintNumber {

	public static void main(String[] args) {
		//a matrix of ROW*ROW
		final int ROW=9;
		
		for (int x = 1; x < ROW+1; x++) {
			for (int y = 1; y < ROW+1; y++) {
				if ((x + y) <= ROW+1) {
					int lineNum = x + y - 1;
					int topOfLast = (lineNum - 1) * (lineNum) / 2;
					boolean direction = lineNum % 2 != 0 ? true : false;
					if (direction) {
						System.out.print(String.format("%3d", (topOfLast + y)));
					} else {
						System.out.print(String.format("%3d", (topOfLast + lineNum - y + 1)));
					}
				} else {
					int innerX = x;
					int innerY = y;
					int offsetX = 1;
					int offsetY = ROW-1;
					innerX -= offsetX;
					innerY -= offsetY;
					int lineNum = innerX + innerY - 1;
					int loopNum = 0;
					int start = ROW-1;
					for (int j = 1; j < lineNum; j++) {
						loopNum += start;
						start--;
					}
					boolean direction = lineNum % 2 == 0 ? true : false;
					if(ROW%2!=0){
						if (direction) {
							System.out.print(String.format("%3d", ROW*ROW-(ROW-(x+y-ROW))*(ROW-(x+y-ROW)+1)/2 +y-ROW));
						} else {
							System.out.print(String.format("%3d", (ROW*ROW-(ROW-(x+y-ROW)+1)*(ROW-(x+y-ROW)+2)/2+1 - y+ROW)));
						}
					}else{
						if (direction) {
							System.out.print(String.format("%3d", (ROW*ROW-(ROW-(x+y-ROW)+1)*(ROW-(x+y-ROW)+2)/2+1 - y+ROW)));
						} else {
							
							System.out.print(String.format("%3d", ROW*ROW-(ROW-(x+y-ROW))*(ROW-(x+y-ROW)+1)/2 +y-ROW));
						}
					}
					
				}
			}
			System.out.println();
		}
	}
}