描述
在n*n方陈里填入1,2,...,n*n,要求填成蛇形。例如n=4时方陈为:10 11 12 19 16 13 28 15 14 37 6 5 4
输入
直接输入方陈的维数,即n的值。(n<=100)
输出
输出结果是蛇形方陈。
样例输入
3
样例输出
7 8 1
6 9 2
5 4 3
//这是自己写出来的算法, 内存和时间都不算最
时间 | 内存 | 语言 |
|
| ||
窗体顶端 104 | 1660 | java | Accepted |
|
|
|
- import java.util.Scanner;
- public class shexingjuzhen {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- if(n>0){
- int[][] zpy = new int[n][n];
- //set 0 of the whole array
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- zpy[i][j] = 0;
- }
- }
- //set number of the northeast to centre diagonal line
- zpy[0][n-1] = 1;
- int m = n-1;
- int h = n-1;
- for (int i = 1; i < n / 2 + n % 2; i++,m--) {
- zpy[i][m - 1] = zpy[i-1][m] + 4 * h ;
- h=h-2;
- }
- //set number of the southeast to centre diagonal line
- zpy[n-1][n-1] = n;
- m=n-1;
- h=n-2;
- for (int i = n-1 ; i >n / 2 + n % 2; i--,m--) {
- zpy[i-1][m - 1] = zpy[i][m] + 4 * h + 2 ;
- h=h-2;
- }
- //set number of the southwest to centre diagonal line
- zpy[n-1][0] = 2*n-1;
- m = 0;
- h = n-2;
- for (int i = n-1 ; i >n / 2 + n % 2; i--,m++) {
- zpy[i-1][m + 1] = zpy[i][m] + 4 * h ;
- h=h-2;
- }
- //set number of the northwest to centre diagonal line
- zpy[0][0] = 3*n-2;
- m = 0;
- h=n-3;
- for (int i = 1; i < n / 2 + n % 2; i++,m++) {
- zpy[i][m+1] = zpy[i-1][m] + 4 * h + 2 ;
- h=h-2;
- }
- // filling the array
- //fill west
- for(int i = 0;i<n/2+n%2;i++){
- if(zpy[i][i] == zpy[n-i-1][i] +n-1-2*i ){
- int mid =zpy[i][i]-1;
- for(int j=i+1;j<n-1-i;j++){
- zpy[j][i] = mid-- ;
- }
- }
- }
- //fill east
- int t= 0;
- for(int i = n-1;i>=n/2+n%2;i--){
- if( zpy[i][i] == zpy[n-i-1][i] +i-t){
- int mid = zpy[i][i]-1;
- for(int j=i-1;j>n-1-i;j--){
- zpy[j][i] = mid--;
- }
- t++;
- }
- }
- //fill north
- for(int i=0;i<n/2+n%2-1;i++){
- for(int j=i;j<n-i-2;j++){
- zpy[i][j+1] = zpy[i][j]+1;
- }
- }
- //fill south
- for(int i=n-1,p=1;i>n/2;i--,p++){
- for(int j=p;j<n-p;j++){
- zpy[i][j] = zpy[i][j-1] - 1;
- }
- }
- // print the array
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- System.out.print(zpy[i][j]+" ");
- }
- System.out.println();
- }
- }else{
- }
- }
- }
下面是一些比较强的算法
| Accepted | 0 | 228 | C/C++ | 11-14 17:29:31 |
- #include"stdio.h"
- int main()
- {
- int a[109][109]={0},i,j,k,n,m,top,x,y;
- top=1;
- scanf("%d",&n);
- a[x=0][y=n-1]=1;
- while(top<n*n)
- {
- while(x+1<n&&!a[x+1][y]) a[++x][y]=++top;
- while(y-1>=0&&!a[x][y-1]) a[x][--y]=++top;
- while(x-1>=0&&!a[x-1][y]) a[--x][y]=++top;
- while(y+1<n&&!a[x][y+1]) a[x][++y]=++top;
- }
- for(i=0;i<n;i++)
- {
- for(j=0;j<n;j++)
- printf("%d ",a[i][j]);
- printf("\n");
- }
- }
| Accepted | 0 | 228 | C/C++ | 11-14 17:32:41 |
- #include<stdio.h>
- int main()
- {
- int n,i,j,a[100][100],m,k=1;
- scanf("%d",&n);
- m=n;
- for(i=0;i<(n+1)/2;i++)
- {
- for(j=i;j<m;j++)
- a[j][m-1]=k++;
- for(j=m-2;j>=i;j--)
- a[m-1][j]=k++;
- for(j=m-2;j>=i;j--)
- a[j][i]=k++;
- for(j=i+1;j<m-1;j++)
- a[i][j]=k++;
- m-=1;
- }
- for(i=0;i<n;i++)
- {
- for(j=0;j<n;j++)
- printf("%d ",a[i][j]);
- printf("\n");
- }
- }
| Accepted | 0 | 264 | C/C++ | 11-14 17:27:57 |
- #include <stdio.h>
- int n;
- int a[10000+1];
- void put(int position,int data)
- {
- a[position]=data;
- }
- int main()
- {
- int i,j,n1,number,position;
- scanf("%d",&n);
- number=n*n;
- if(n>100) n=100;
- else if(n<1) n=1;
- n1=n;
- i=1;
- position=0;
- while(i<=number)
- {
- for(j=1;j<=n1;j++)
- {
- position+=n;
- put(position,i);
- i++;
- }
- for(j=1;j<n1;j++)
- {
- position-=1;
- put(position,i);
- i++;
- }
- for(j=1;j<n1;j++)
- {
- position-=n;
- put(position,i);
- i++;
- }
- for(j=1;j<n1-1;j++)
- {
- position+=1;
- put(position,i);
- i++;
- }
- n1-=2;
- }
- int d;
- for(i=0;i<n;i++)
- {
- for(j=0;j<n;j++)
- {
- d=a[i*n+j+1];
- if(d<10) printf("%d ",d);
- else if(d<100) printf("%d ",d);
- else if(d<1000) printf("%d ",d);
- else if(d<10000) printf("%d ",d);
- else printf("%d ",d);
- }
- printf("\n");
- }
- }
- 284079 28210913 蛇形填数 Accepted 0 228 C/C++ 11-14 17:29:31
284079 | Accepted | 0 | 228 | C/C++ | 11-14 17:29:31 |