Title address: http://acm.nyist.net/JudgeOnline/problem.php?pid=33?? Describe
In the N*n Fang Chen filled in,..., n*n, asked to fill the snake. For example n=4 when Fangchen is:
10 11 12 1
9 16 13 2
8 15 14 3
7 6 5 4
Input
The dimension of the direct input square, that is, the value of N. (n<=100)
Output
The result is a serpentine phalanx.
Sample input
3
Sample output
7 8 1
6 9 2
5 4 3? #include <stdio.h>
#include <stdlib.h>
static void Setarr (int **arr,int readnum);
static void Printarr (int **arr,int num);
int main ()
{
???? int readnum = 0;
???? scanf ("%d", &readnum);
???? GetChar ();
????
???? int **arr = (int**) malloc (sizeof (int*) *readnum);
???? int rowIndex = 0;
???? for (; RowIndex < Readnum;++rowindex)
???? {
????????? Arr[rowindex] = (int*) malloc (sizeof (int) *readnum);
???? }
????
???? Setarr (Arr,readnum);
???? Printarr (Arr,readnum);
?????????
???? RowIndex = 0;
???? for (; RowIndex < Readnum;++rowindex)
???? {
????????? Free (Arr[rowindex]);
???? }
???? Free (arr);
????
????
???? return 0;
}
static void Setarr (int **arr,int readnum)
{
???? int i=0;
???? int flag = Readnum;
???? int colindex = readNum-1;
???? int rowIndex = 0;
???? int maxnum = Readnum*readnum;
???? while (I<maxnum)//If it is an even number, the maximum value is ended when the upper side is executed.
???? {
????????? Colindex = flag-1;
?????????
????????? if (i = = maxNum-1)//If it is an odd square, the last number is the last maximum value, and the end after special processing
????????? {
?????????????? Arr[rowindex][colindex] = ++i;
?????????????? Break
????????? }
?????????
????????? Right, row +, column unchanged
????????? for (; rowindex<flag-1 && I<=maxnum;++rowindex)
????????? {
?????????????? Arr[rowindex][colindex] = ++i;
????????? }
?????????
????????? Lower side, row invariant column-
????????? for (; Colindex>readnum-flag && I<=maxnum;--colindex)
????????? {
?????????????? Arr[rowindex][colindex] = ++i;
????????? }
?????????
????????? Left, row-, column unchanged
????????? for (; Rowindex>readnum-flag && I<=maxnum;--rowindex)
????????? {
?????????????? Arr[rowindex][colindex] = ++i;
????????? }
?????????
????????? Upper side, row unchanged, column + +
????????? for (; colindex<flag-1 && I<=maxnum;++colindex)
????????? {
?????????????? Arr[rowindex][colindex] = ++i;
????????? }
?????????
????????? --flag;
????????? ++rowindex;
???? }
}
static void Printarr (int **arr,int num)
{
???? Int???? RowIndex = 0;
???? for (; rowindex<num;++rowindex)
???? {
????????? int colindex = 0;
????????? for (; Colindex < Num;++colindex)
????????? {
?????????????? printf ("%d", Arr[rowindex][colindex]);
????????? }
????????? printf ("\ n");
???? }
}
09-Language Primer -09-Snake fill number