Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class scorify{
- public static void main (String[] args) {
- Scanner in= new Scanner(System.in);
- while(in.hasNext()){
- int N=in.nextInt();
- int[][] t=new int[N][N];
- int xp=N/2+1,yp=xp;
- int n=1;
- t[xp][yp]=n;
- int up=1;
- int j=1;
- boolean b=false;
- while( true){
- j++;
- if(up==1){
- for(int i=1;i<j;i++){
- n++;
- xp+=1;
- if(xp==N){b=true; break;}
- t[xp][yp]=n;
- }
- for(int i=1;i<j;i++){
- n++;
- yp-=1;
- if(yp<=-1) { b=true; break;}
- t[xp][yp]=n;
- }
- if(b) break;
- up=0;
- continue;
- }
- else if(up==0){
- for(int i=1;i<j;i++){
- n++;
- xp-=1;
- if(xp<=-1) { b=true; break;}
- t[xp][yp]=n;
- }
- for(int i=1;i<j;i++){
- n++;
- yp+=1;
- if(yp==N) { b=true; break;}
- t[xp][yp]=n;
- }
- if(b) break;
- up=1;
- }
- }
- for(int r=0 ; r< N ; r++ ){
- for(int k=0; k<N ; k++ ){
- System.out.print(t[r][k]+" ");
- }
- System.out.println();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement