Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class scorify{
  4. public static void main (String[] args) {
  5. Scanner in= new Scanner(System.in);
  6. while(in.hasNext()){
  7. int N=in.nextInt();
  8. int[][] t=new int[N][N];
  9. int xp=N/2+1,yp=xp;
  10. int n=1;
  11. t[xp][yp]=n;
  12. int up=1;
  13. int j=1;
  14. boolean b=false;
  15. while( true){
  16. j++;
  17. if(up==1){
  18. for(int i=1;i<j;i++){
  19. n++;
  20. xp+=1;
  21. if(xp==N){b=true; break;}
  22. t[xp][yp]=n;
  23. }
  24. for(int i=1;i<j;i++){
  25. n++;
  26. yp-=1;
  27. if(yp<=-1) { b=true; break;}
  28. t[xp][yp]=n;
  29. }
  30. if(b) break;
  31. up=0;
  32. continue;
  33. }
  34. else if(up==0){
  35. for(int i=1;i<j;i++){
  36. n++;
  37. xp-=1;
  38. if(xp<=-1) { b=true; break;}
  39. t[xp][yp]=n;
  40. }
  41. for(int i=1;i<j;i++){
  42. n++;
  43. yp+=1;
  44. if(yp==N) { b=true; break;}
  45. t[xp][yp]=n;
  46. }
  47. if(b) break;
  48. up=1;
  49. }
  50.  
  51. }
  52. for(int r=0 ; r< N ; r++ ){
  53. for(int k=0; k<N ; k++ ){
  54. System.out.print(t[r][k]+" ");
  55. }
  56. System.out.println();
  57. }
  58. }
  59.  
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement