Skip to content

Instantly share code, notes, and snippets.

@frojasg
Created January 10, 2011 12:07
Show Gist options
  • Save frojasg/772690 to your computer and use it in GitHub Desktop.
Save frojasg/772690 to your computer and use it in GitHub Desktop.
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