题解 | #蛇形矩阵# 试了下不用二维数组,结果本质上来说没太大区别。
蛇形矩阵
http://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
int num = Integer.parseInt(sc.nextLine());
if(num<=0){
System.out.println();
}else{
int rowInitNum = 1;
int rowInitPlusNum = 2;
int colInitPlusNum = 1;
for(int i=0; i<num; i++){
int rowInitNumTmp = rowInitNum;
int rowInitPlusNumTmp = rowInitPlusNum;
for(int j=0; j<num-i; j++){
System.out.print(rowInitNumTmp+" ");
rowInitNumTmp += rowInitPlusNumTmp;
rowInitPlusNumTmp++;
}
rowInitNum = rowInitNum + colInitPlusNum;
colInitPlusNum++;
rowInitPlusNum++;
System.out.println();
}
}
}
}
}
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
int num = Integer.parseInt(sc.nextLine());
if(num<=0){
System.out.println();
}else{
int rowInitNum = 1;
int rowInitPlusNum = 2;
int colInitPlusNum = 1;
for(int i=0; i<num; i++){
int rowInitNumTmp = rowInitNum;
int rowInitPlusNumTmp = rowInitPlusNum;
for(int j=0; j<num-i; j++){
System.out.print(rowInitNumTmp+" ");
rowInitNumTmp += rowInitPlusNumTmp;
rowInitPlusNumTmp++;
}
rowInitNum = rowInitNum + colInitPlusNum;
colInitPlusNum++;
rowInitPlusNum++;
System.out.println();
}
}
}
}
}