09-Language Primer -09-Snake fill number

Source: Internet
Author: User

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

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.