阅读背景:

由数字对角线组成的三角形

来源:互联网 
/**
 * 示例:<br>
 * 1 3 6 10 15 21 28 36 45 55 66 78<br>
 * 2 5 9 14 20 27 35 44 54 65 77<br>
 * 4 8 13 19 26 34 43 53 64 76<br>
 * 7 12 18 25 33 42 52 63 75<br>
 * 11 17 24 32 41 51 62 74<br>
 * 16 23 31 40 50 61 73<br>
 * 22 30 39 49 60 72<br>
 * 29 38 48 59 71<br>
 * 37 47 58 70<br>
 * 46 57 69<br>
 * 56 68<br>
 * 67<br>
 * 
 * @author LSJ
 *
 */
public class Sanjiaoxing {
	public static void main(String[] args) {
		new Sanjiaoxing().f(12);
	}

	/**
	 * 方法1
	 * 
	 * @param x
	 */
	public void f1(int x) {
		int[][] temp = new int[x][x];
		int n = 0;
		int r = 0;
		int xx = 0;
		int yy = 0;
		while (true) {
			temp[yy--][xx++] = n++;
			if (yy < 0) {
				xx = 0;
				yy = r++;
			}
			if (yy >= x) {
				break;
			}
		}
		/** 打印输出 */
		for (int i = 0; i < temp.length; i++) {
			for (int j = 0; j < temp[0].length; j++) {
				if (temp[i][j] != 0)// 不为0输出
					System.out.print(temp[i][j] + "\t");
			}
			System.out.println();
		}
	}

	public void f(int x) {
		int[][] temp = new int[x][x];
		temp[0][0] = 1;
		int t_x = x;
		int t_y = x;
		int r = 0;// 行数初始值
		int c = 0;
		for (int i = 0; i < t_x; i++) {
			if (i != 0)
				temp[i][0] = temp[i - 1][0] + (++r);
			c = i + 2;
			for (int j = 1; j < t_y; j++) {
				temp[i][j] = temp[i][j - 1] + (c++);

			}
			t_y--;
		}
		/** 打印输出 */
		for (int i = 0; i < temp.length; i++) {
			for (int j = 0; j < temp[0].length; j++) {
				if (temp[i][j] != 0)// 不为0输出
					System.out.print(temp[i][j] + "\t");
			}
			System.out.println();
		}
	}
}
/**
 * 示例:<br>
 * 1 3 6 10 15 21 28 36 45 55 66 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: