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


/**
* 示例:<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();
}
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值