24: Serpentine padding array
- View
- Submit
- Statistics
- Questions
-
Total time limit:
-
1000ms
-
Memory Limit:
-
65536kB
-
-
Describe
-
-
With digital 1,2,3,4,..., n*n This n2 number of snakes filled with the scale of N*n Square.
The Serpentine Fill method is:
For each lower left-right diagonal, numbered from top left to bottom right,..., 2n-1; According to the order from small to large, the numbers from small to large to fill in each slash, where numbered odd from the left to the right to fill, numbered even from the top right to the left to fill.
For example, when n=4, the square is filled with the following form:
1 2 6 73 5 8 134 9 12 1410 11 15 16
-
-
Input
-
-
Enter a positive integer not greater than 10 N, representing the number of rows of the square.
-
-
Output
-
-
The matrix is output, with a single space interval between adjacent two elements.
-
-
Sample input
-
-
4
-
-
Sample output
-
1 2 6 73 5 8 134 9 12 1410 11 15 16
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <cstring>5 using namespacestd;6 inttot=1;7 intans=2;8 intnow=1;//1 down 2 up9 inta[101][101];Ten intMain () One { A intN; -Cin>>N; - intI=1, j=2; thecout<<1<<" "; - while(tot!=n*N) - { - if(now==1) + { - +a[i][j]=ans; Aans++; attot++; - if(j==1|| i==N) - { - if(j==1&&i!=N) -i++; - ElseJ + +; innow=2; - Continue; to } + Else - { thei++; *j--; $ }Panax Notoginseng - } the if(now==2) + { A thea[i][j]=ans; +ans++; -tot++; $ if(i==1|| j==N) $ { - if(i==1&&j!=N) -J + +; the Elsei++; -now=1;Wuyi Continue; the } - Else Wu { -i--; AboutJ + +; $ } - } - Elsenow=1; - } A for(intI=1; i<=n;i++) + { the for(intj=1; j<=n;j++) - { $ if(i==1&&j==1)Continue; the Else thecout<<a[i][j]<<" "; the } thecout<<Endl; - } in return 0; the}
24: Serpentine padding array