Skip to content

Instantly share code, notes, and snippets.

@frojasg
Created Jan 10, 2011
Embed
What would you like to do?
zig-zag
public class Matrix {
public static void main(String [] args){
int n = Integer.parseInt(args[0]);
int[][] A = new int[n][n];
int count = 0;
for(int i = 0; i< n; i++) {
for(int j = i, z=0; j>=0 && z <= i; j--, z++){
if(i%2==1) {
A[z][j]=count;
if(!(j==n-1 || z==n-1))
A[n-1-z][n-1-j]=n*n-1-count;
}else {
A[j][z]=count;
if(!(j==n-1 || z==n-1))
A[n-1-j][n-1-z]=n*n-1-count;
}
count++;
}
}
for(int i = 0; i< A.length; i++) {
for(int j = 0; j< A.length; j++) {
System.out.print(A[i][j]+" ");
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment