题解 | #蛇形矩阵#
蛇形矩阵
http://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
// 输出值
int y = 1; //列起始值
int yCount = 1; //列间隔起始值
// 外层控制行,内层控制列
for(int i = 1;i <= n;i++){
int x = y; //行起始值
int xCount = i + 1; //行间隔起始值
for(int j = 1 ;j <= n-i+1;j++){ //列循环次数
if(j == n-i+1){
System.out.println(x);
}else{
System.out.print(x + " ");
}
x = x + xCount;
xCount++;
}
y = y + yCount;
yCount++;
}
}
}
}