描述

n*n方陈里填入1,2,...,n*n,要求填成蛇形。例如n=4时方陈为:
10 11 12 1
9 16 13 2
8 15 14 3
7 6 5 4

输入

直接输入方陈的维数,即n的值。(n<=100)

输出

输出结果是蛇形方陈。

样例输入

3

样例输出

7 8 1

6 9 2

5 4 3

//这是自己写出来的算法, 内存和时间都不算最

时间

内存

语言

窗体顶端

104

1660

java

Accepted

 

 
   
  1. import java.util.Scanner;  
  2.  
  3. public class shexingjuzhen {  
  4.  public static void main(String[] args) {  
  5.   Scanner scanner = new Scanner(System.in);  
  6.     
  7.   int n = scanner.nextInt();  
  8.   if(n>0){  
  9.     
  10.    int[][] zpy = new int[n][n];  
  11.   //set  0 of the whole array  
  12.   for(int i=0;i<n;i++){  
  13.    for(int j=0;j<n;j++){  
  14.     zpy[i][j] = 0;  
  15.    }  
  16.   }  
  17.   //set number of the northeast to centre diagonal line   
  18.   zpy[0][n-1] = 1;  
  19.   int m = n-1;  
  20.   int h = n-1;  
  21.   for (int i = 1; i < n / 2 + n % 2; i++,m--) {  
  22.    zpy[i][m - 1] = zpy[i-1][m] + 4 * h ;  
  23.    h=h-2;  
  24.      
  25.   }  
  26.     
  27.   //set number of the southeast to centre diagonal line  
  28.   zpy[n-1][n-1] = n;  
  29.   m=n-1;  
  30.   h=n-2;  
  31.   for (int i = n-1 ; i >n / 2 + n % 2; i--,m--) {  
  32.    zpy[i-1][m - 1] = zpy[i][m] + 4 * h + 2 ;  
  33.    h=h-2;  
  34.      
  35.   }  
  36.     
  37.   //set number of the southwest to centre diagonal line  
  38.   zpy[n-1][0] = 2*n-1;  
  39.   m = 0;  
  40.   h = n-2;  
  41.   for (int i = n-1 ; i >n / 2 + n % 2; i--,m++) {  
  42.    zpy[i-1][m + 1] = zpy[i][m] + 4 * h ;  
  43.    h=h-2;  
  44.      
  45.   }  
  46.     
  47.   //set number of the northwest to centre diagonal line  
  48.   zpy[0][0] = 3*n-2;  
  49.   m = 0;  
  50.   h=n-3;  
  51.   for (int i = 1; i < n / 2 + n % 2; i++,m++) {  
  52.    zpy[i][m+1] = zpy[i-1][m] + 4 * h + 2 ;  
  53.    h=h-2;  
  54.      
  55.   }  
  56.     
  57.   // filling the array  
  58.           //fill west  
  59.   for(int i = 0;i<n/2+n%2;i++){  
  60.    if(zpy[i][i] == zpy[n-i-1][i] +n-1-2*i  ){  
  61.     int mid =zpy[i][i]-1;  
  62.     for(int j=i+1;j<n-1-i;j++){  
  63.      zpy[j][i] = mid-- ;  
  64.     }  
  65.    }   
  66.   }  
  67.     //fill east  
  68.   int t= 0;  
  69.   for(int i = n-1;i>=n/2+n%2;i--){  
  70.    if( zpy[i][i] == zpy[n-i-1][i] +i-t){  
  71.     int mid = zpy[i][i]-1;  
  72.     for(int j=i-1;j>n-1-i;j--){  
  73.        
  74.      zpy[j][i] = mid--;  
  75.        
  76.     }  
  77.     t++;  
  78.    }   
  79.   }  
  80.     
  81.     //fill north  
  82.   for(int i=0;i<n/2+n%2-1;i++){  
  83.    for(int j=i;j<n-i-2;j++){  
  84.     zpy[i][j+1] = zpy[i][j]+1;  
  85.    }  
  86.   }  
  87.     
  88.     //fill south  
  89.   for(int i=n-1,p=1;i>n/2;i--,p++){  
  90.      
  91.    for(int j=p;j<n-p;j++){  
  92.     zpy[i][j] = zpy[i][j-1] - 1;  
  93.    }  
  94.   }  
  95.     
  96.   // print the array  
  97.   for(int i=0;i<n;i++){  
  98.    for(int j=0;j<n;j++){  
  99.     System.out.print(zpy[i][j]+" ");  
  100.    }  
  101.    System.out.println();  
  102.   }  
  103.   }else{  
  104.      
  105.   }  
  106.  }  
  107. }  

 

 

 

下面是一些比较强的算法

蛇形填数

Accepted

0

228

C/C++

11-14 17:29:31

 
   
  1. #include"stdio.h"  
  2. int main()  
  3. {  
  4. int a[109][109]={0},i,j,k,n,m,top,x,y;  
  5. top=1;  
  6. scanf("%d",&n);  
  7. a[x=0][y=n-1]=1;  
  8. while(top<n*n)  
  9. {  
  10. while(x+1<n&&!a[x+1][y]) a[++x][y]=++top;  
  11. while(y-1>=0&&!a[x][y-1]) a[x][--y]=++top;  
  12. while(x-1>=0&&!a[x-1][y]) a[--x][y]=++top;  
  13. while(y+1<n&&!a[x][y+1]) a[x][++y]=++top;  
  14.  
  15. }  
  16.  
  17. for(i=0;i<n;i++)  
  18. {  
  19. for(j=0;j<n;j++)  
  20.  
  21. printf("%d ",a[i][j]);  
  22. printf("\n");  
  23. }  

蛇形填数

Accepted

0

228

C/C++

11-14 17:32:41

 

 

 
   
  1. #include<stdio.h>  
  2. int main()  
  3. {  
  4. int n,i,j,a[100][100],m,k=1;  
  5.     scanf("%d",&n);  
  6.     m=n;  
  7. for(i=0;i<(n+1)/2;i++)  
  8.     {  
  9. for(j=i;j<m;j++)  
  10.             a[j][m-1]=k++;  
  11. for(j=m-2;j>=i;j--)  
  12. a[m-1][j]=k++;  
  13. for(j=m-2;j>=i;j--)  
  14. a[j][i]=k++;  
  15. for(j=i+1;j<m-1;j++)  
  16.             a[i][j]=k++;  
  17.         m-=1;  
  18.     }  
  19. for(i=0;i<n;i++)  
  20.     {  
  21. for(j=0;j<n;j++)  
  22.             printf("%d ",a[i][j]);  
  23.         printf("\n");  
  24.     }  
  25. }  
  26.  

蛇形填数

Accepted

0

264

C/C++

11-14 17:27:57

 

 
   
  1. #include <stdio.h>  
  2. int n;  
  3. int a[10000+1];  
  4. void put(int position,int data)  
  5. {  
  6. a[position]=data;  
  7. }  
  8. int main()  
  9. {  
  10. int i,j,n1,number,position;  
  11. scanf("%d",&n);  
  12. number=n*n;  
  13. if(n>100) n=100;  
  14. else if(n<1) n=1;  
  15. n1=n;  
  16. i=1;  
  17. position=0;  
  18. while(i<=number)  
  19. {  
  20. for(j=1;j<=n1;j++)  
  21. {  
  22. position+=n;  
  23. put(position,i);  
  24. i++;  
  25. }  
  26. for(j=1;j<n1;j++)  
  27. {  
  28. position-=1;  
  29. put(position,i);  
  30. i++;  
  31. }  
  32. for(j=1;j<n1;j++)  
  33. {  
  34. position-=n;  
  35. put(position,i);  
  36. i++;  
  37. }  
  38. for(j=1;j<n1-1;j++)  
  39. {  
  40. position+=1;  
  41. put(position,i);  
  42. i++;  
  43. }  
  44. n1-=2;  
  45. }  
  46. int d;  
  47. for(i=0;i<n;i++)  
  48. {  
  49. for(j=0;j<n;j++)  
  50. {  
  51. d=a[i*n+j+1];  
  52. if(d<10) printf("%d ",d);  
  53. else if(d<100) printf("%d ",d);  
  54. else if(d<1000) printf("%d ",d);  
  55. else if(d<10000) printf("%d ",d);  
  56. else printf("%d ",d);  
  57. }  
  58. printf("\n");  
  59. }  
  60. }  
  61.  
  62. 284079 28210913 蛇形填数 Accepted  0  228 C/C++ 11-14 17:29:31   
  63.  

28407928210913蛇形填数Accepted0228C/C++11-14 17:29:31