Codevs 1160 Serpentine Matrix

Source: Internet
Author: User

time limit: 1 sspace limit: 128000 KBTitle Description Description

Xiaoming play a number game, take an n-row N-column number matrix (where n is not more than 100 odd), the number is filled by: in the center of the matrix from 1 to the counterclockwise direction, the circle expands, until n row n column fills the number, output the n row n column square matrix and its diagonal number of the sum.

Enter a description Input Description

n (i.e. n rows n columns)

Output description Output Description

N+1 line, n-Behavior matrix, the sum of the last behavior diagonal numbers

Sample input Sample Input

3

Sample output Sample Output

5 4 3
6 1 2
7 8 9
25

#include <cstdio>#include<cmath>#include<cctype>#include<iostream>#include<vector>#include<cstring>#include<algorithm>#include<map>#include<Set>#include<cstddef>#include<sstream>#include<cstdlib>#include<stack>#include<queue>using namespacestd;//I was going to fill the array with the circle, but I never found it. And then we just output it.//looking for the relationship between subscript and element, this is a math problem. intMain () {intN; CIN>>N; if(0= = n%2)return 0;//Although data is impossible to contain even numbers.     intM, ctr = n/2; intsum =0, T;  for(inti =0; I < n; i++) {         for(intj =0; J < N; J + +) {//output sequentiallym = Max (ABS (I-CTR), ABS (J-CTR));//ABS is absolute,m =2* m +1; if((ctr-i) = = (M-1)/2) || (ctr-j) = = (M-1)/2)) T= m*m-2*m-j+i+2; Else if(i-ctr) = = (M-1)/2) T= m*m-m+1+j-2*ctr+i; ElseT= m*m-3*m+3+2*ctr-i-K; cout<< T <<" "; if(I==j | | (i+j) = = (n1))//DiagonalSum + =T; } cout<<Endl; } cout<< sum <<Endl; return 0;}

Copy to Google TranslateTranslation Results

Codevs 1160 Serpentine Matrix

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.