22: Magic magic, 22: Magic magic

Source: Internet
Author: User

22: Magic magic, 22: Magic magic
22: Magic magic

  • View
  • Submit
  • Statistics
  • Question
Total time limit:
1000 ms
 
Memory limit:
65535kB
Description

Magic is a magical N * N matrix. Each row, column, and diagonal line of the matrix share the same sum of numbers.
We can build a magic square using the following methods. (Odd order number)
1. Write the first number in the middle of the first line
2. The next number is written in the upper right corner of the previous number:
A. If the number is in the first row, the next number is written in the last row, and the number of columns is the right column of the number.
B. If the number is in the last column, the next number is written in the first column, and the number of rows is the last row of the number.
C. If the number is in the upper-right corner or there is a number in the upper-right corner of the number, the next number is written below the number.

Input
One digit N (N <= 20)
Output
2N-1*2N-1 Magic Square Constructed by the above method
Sample Input
3
Sample output
17 24 1 8 1523 5 7 14 164 6 13 20 2210 12 19 21 311 18 25 2 9


 1 #include<iostream> 2 using namespace std; 3 int a[1001][1001]; 4 int now=1; 5 int tot=0; 6 int hang,lie; 7 int main()  8 { 9     int n;10     cin>>n;11     int c=2*n-1;12     while(tot!=(c*c))13     {14         if(tot==0)15         {16             a[1][c/2+1]=now;17             now++;18             hang=1;19             lie=c/2+1;20             tot++;21         }22         else if(hang==1&&lie!=c)23         {24             a[c][lie+1]=now;25             now++;26             tot++;27             hang=c;28             lie=lie+1;29         }30         else if(lie==c&&hang!=1)31         {32             a[hang-1][1]=now;33             now++;34             tot++;35             hang=hang-1;36             lie=1;37         }38         else if((hang==1&&lie==c)||(a[hang-1][lie+1]!=0))39         {40             a[hang+1][lie]=now;41             now++;42             tot++;43             hang=hang+1;44         }45         else 46         {47             a[hang-1][lie+1]=now;48             now++;49             tot++;50             hang=hang-1;51             lie=lie+1;52         }53     }54     for(int i=1;i<=c;i++)55     {56         for(int j=1;j<=c;j++)57         {58             cout<<a[i][j]<<" ";59         }60         cout<<endl;61     }62     return 0;63 }

Simulation .,,,,,,,,,,,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.