现给出了第一种矩阵解法,后面的解法会补上
package com.test;
public class OutNumber {
/**
* @param args
* 1234
* 4568
* 78910//第一种矩阵
*
* 1234567
* 1234567//第二种矩阵
*/
public static void main(String[] args) {
int a[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
// int a[][]={{1,2,3,4,5},{6,7,8,9,10}};
int flag = 0;
for (int j = 0; j < a.length; j++) {
int k = 0;
flag = j;
while (k > -1 && k <= j) {
System.out.print(a[flag][k] + " ");
flag--;
k++;
}
System.out.println();
}
int count = 1;// 用来控制数量和最大值
int k1 = a.length - 1;
while (k1 >= count) {
for (int j = count; j < a.length; j++) {
System.out.print(a[k1][j] + " ");
k1--;
}
System.out.println();
count++;
k1 = a.length - 1;// 从新开始遍历
}
}
}