cogs 2104. [NOIP2015]神奇的幻方

★   输入文件:2015magic.in   输出文件:2015magic.out   简单对比
时间限制:1 s   内存限制:256 MB

 1 #include<iostream>
 2 #include<cstdio>
 3 int a[41][41];
 4 int main() 
 5 {
 6     freopen("2015magic.in","r",stdin);
 7     freopen("2015magic.out","w",stdout);
 8     int n;
 9     int x=0,y=0;
10     int t=1;
11     scanf("%d",&n);
12     n=(n+1)/2;
13     while(t<=(2*n-1)*(2*n-1)) 
14     {
15         if(t==1) 
16         {
17             a[1][n]=1;
18             t++;
19             x=1;
20             y=n;
21         }
22         if(x==1&&y!=2*n-1) 
23         {
24             a[2*n-1][y+1]=t;
25             t++;
26             x=2*n-1;
27             y++;
28         }
29         if(x!=1&&y==2*n-1)
30          {
31             a[x-1][1]=t;
32             t++;
33             x--;
34             y=1;
35         }
36         if((x==1&&y==2*n-1)||a[x-1][y+1]!=0) 
37         {
38             a[x+1][y]=t;
39             t++;
40             x++;
41         }
42         if(a[x-1][y+1]==0&&x!=1&&y!=2*n-1) 
43         {
44             a[x-1][y+1]=t;
45             t++;
46             x--;
47             y++;
48         }
49     }
50     for(int i=1; i<=2*n-1; i++) 
51     {
52         for(int j=1; j<=2*n-1; j++) 
53         {
54             printf("%d ",a[i][j]);
55         }
56         printf("
");
57     }
58     return 0;
59 }
原文地址:https://www.cnblogs.com/lyqlyq/p/6863555.html