1160 snake matrix, 1160 snake

Source: Internet
Author: User

1160 snake matrix, 1160 snake
1160 snake Matrix

 

Time Limit: 1 s space limit: 128000 KB title level: Silver QuestionDescription Description

James plays a digital game and obtains an n-row and n-column Number Matrix (where n is an odd number not greater than 100). The number filling method is as follows: in the center of the matrix, start from 1 and circle it in a counter-clockwise direction, expanding circle by circle until n rows and n columns are filled with numbers. Please output the square matrix of n rows and the sum of its diagonal numbers.

Input description Input Description

N (n rows and n columns)

Output description Output Description

A matrix composed of n + 1 rows and n actions. The last row is the sum of diagonal numbers.

Sample Input Sample Input

3

Sample output Sample Output

5 4 3
6 1 2
7 8 9
25

1 # include <iostream> 2 using namespace std; 3 int now = 1; 4 int a [101] [101]; 5 int s = 0; 6 int fx = 1; // 1 right 2 left 3 up 4 down 7 int tot = 1; 8 int ans = 0; 9 int main () 10 {11 int n; 12 cin> n; 13 s = n/2 + 1; 14 int I = s; 15 int j = s; 16 a [I] [j] = now; 17 now ++; 18 while (tot! = N * n) 19 {20 if (fx = 1 & j-I = 1) 21 {22 fx = 3; 23 // a [I] [j] = now; 24 // now ++; 25 // tot ++; 26} 27 if (fx = 2 & I = j) 28 {29 fx = 4; 30 // a [I] [j] = now; 31 // now ++; 32 // tot ++; 33} 34 if (fx = 3 & (I + j = n + 1 )) 35 {36 fx = 2; 37 // a [I] [j] = now; 38 // now ++; 39 // tot ++; 40} 41 if (fx = 4 & (I + j = n + 1) 42 {43 fx = 1; 44 // a [I] [j] = now; 45 // now ++; 46 // tot ++; 47} 48 if (fx = 1) // 1 right 2 left 3 Top 4 bottom 49 {50 j ++; 51 a [I] [j] = now; 52 now ++; 53 tot ++; 54} 55 if (fx = 2) // 1 right 2 left 3 Top 4 bottom 56 {57 j --; 58 a [I] [j] = now; 59 now ++; 60 tot ++; 61} 62 if (fx = 3) // 1 right 2 left 3 Top 4 lower 63 {64 I --; 65 a [I] [j] = now; 66 now ++; 67 tot ++; 68} 69 if (fx = 4) // 1 right 2 left 3 Top 4 bottom 70 {71 I ++; 72 a [I] [j] = now; 73 now ++; 74 tot ++; 75} 76} 77 for (int I = 1; I <= n; I ++) 78 {79 for (int j = 1; j <= n; j ++) 80 {81 cout <a [I] [j] <"; 82 if (I + j = n + 1) | (I = j )) 83 ans = ans + a [I] [j]; 84} 85 cout <endl; 86} 87 cout <ans; 88 return 0; 89}

 

Related Article

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.