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