/*
2、	给定一个正整数n,输出如下 n * n “之字形”方阵。
例: 7 *7方阵。
   1   2   6   7  15  16  28
   3   5   8  14  17  27  29
   4   9  13  18  26  30  39
  10  12  19  25  31  38  40
  11  20  24  32  37  41  46
  21  23  33  36  42  45  47
  22  34  35  43  44  48  49


*/
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
int main()
{
    int num;//num是我们需要输出方阵德行列数,num行num列
     printf("请输入规模(行列数):\n");
    scanf("%d",&num);
    printf("\n");
   int **a = (int **)malloc(sizeof(int *) * (num+1));
   //用一个二级指针来实现定义一个可动态分配内存的二维数组,即指针的指针
   //并给二级指针分配空间
for (int i = 0; i <(num+1); ++i)
//为二级指针里面的指针分配空间
	{
		a[i] = (int *)malloc(sizeof(int) * (num+1));
	}
    int i,j,temp=0,tempj,tempi;//i,j是行数和列数
    //tempj,tempi是后面用的临时变量,表示从当前行数或列数开始向右上或左下填数
    //temp是储存当前或上一个填空的数值,每填一个加1
    for(i=0;i<=num;i++)//把数组初始化为0
    {
   
    for(j=0;j<=num;j++)
    {
        a[i][j]=0;
    }
}  
 a[1][1]=1;//第一个值为1
i=1;//从第一列开始
    for(j=1;j<=num;j++)//这个大循环处理的是左上三角
    {
        if(j%2==0)//这个判断可以没有,因为总是从第一个数开始
        //第一行偶数列值是前一列的值加1
        {
            
            a[i][j]=a[i][j-1]+1;//第一行偶数列值是前一列的值加1
            temp=a[i][j];//更新temp
            tempj=j;//用tempj记住当前第一行的列数,因为j在下面的循环中会改变
            for(int n=1;n<=tempj-1;n++)//考虑每向左下运动一次记为操作一次
            //一共操作列号(即是第一行第tempj列)-1次到达第第tempj行第一列
            {
                j--;
                i++;//向左下运动
                 temp=temp+1;//temp更新
                a[i][j]=temp;
            }//执行完此循环已经到哒了第一列
                i++;//达到第一列,运动到下一行
                if(i<=num)//判断有没有超过最大行数,超过直接跳出大循环开始下半三角
            {
                 temp=temp+1;//更新temp
                a[i][j]=temp;
                tempi=i;//用tempi记住当前第一列的行号
            for(int n=1;n<=tempi-1;n++)//每向右上运动一次记为操作一次
            //一共操作记住的行号-1次到达第一行
            {
                j++;
                i--;//向右上运动
                 temp=temp+1;
                a[i][j]=temp;
            }

            }
            else 
            {
            break;//超过直接跳出大循环开始下半三角
            }
                      
        }
    }


//这里开始处理下半三角,这里分两种情况,一种情况是一共有偶数列(行),一种
//是奇数列(行),分别对应j和i在上一部分处理完会超过最大设置的列(行)数,
//这里两种判断都可以,这里选择判断奇偶
    if(num%2==0)//若有偶数列(行)则上一部分处理完后行号i肯定会超出最大的,且数组越界
    {   i--;//故需要回溯到原来的行号
        j++;//再往右边走一个,
        //此时才处于下三角的最左下角
        for(;j<=num;j++)//大部分原理同上
        //不同的是,现在的大循环是看最后一行
        {
            if(j%2==0)
            {
                  
            a[i][j]=a[i][j-1]+1;
            temp=a[i][j];
            tempj=j;
            for(int n=1;n<=num-tempj;n++)//右上走
            {
                j++;
                i--;
                 temp=temp+1;
                a[i][j]=temp;
            }
             i++;//到达最后一列,运动到下一行
                if(i<=num)
            {
                 temp=temp+1;
                a[i][j]=temp;
                tempi=i;
            for(int n=1;n<=num-tempi;n++)//左下走
            {
                j--;
                i++;
                 temp=temp+1;
                a[i][j]=temp;
            }

            }
            else 
            {
            break;
            }
                   
            }
            
        }
    }

     if(num%2!=0)//若有奇数列(行)则上一部分处理完后行号j肯定会超出最大的,且数组越界
     {
         j--;//故需要回溯到原来的列号
         i++;
         //此时处于最下三角最右上角
         for(;i<=num;i++)
        {
            if(i%2==0)
            {
                  
            a[i][j]=a[i-1][j]+1;
            temp=a[i][j];
                tempi=i;
               for(int n=1;n<=num-tempi;n++)//往左下走
             {
                j--;
                i++;
                 temp=temp+1;
                a[i][j]=temp;
             }//到了第一列
             j++;
                if(j<=num)
             {
                 temp=temp+1;
                a[i][j]=temp;
                tempj=j;
             for(int n=1;n<=num-tempj;n++)//往右上走
                {
                j++;
                i--;
                 temp=temp+1;
                a[i][j]=temp;
                }

             }
             else 
                {
                break;
                }
            }
        }
    
     }


     for(i=1;i<=num;i++)//输出二维数组
     {
         for(j=1;j<=num;j++)
         {
             printf("%4d",a[i][j]);
         }
         printf("\n");
     }
     system("pause");

}

4 条评论

erotik · 2021年3月2日 上午3:23

I always used to study paragraph in news papers but now as I am a user of web therefore from now I am using net for content, thanks to web. Gwendolin Rip Tawsha Marje Hillie Hendrickson

bursa escort · 2021年3月2日 上午4:31

Everything is very open with a precise description of the challenges. It was truly informative. Your website is very useful. Thanks for sharing! Jacynth Tremain Muraida

erotik · 2021年3月2日 上午10:30

Nihil perferendis quo et eius nihil voluptatibus exercitationem. Aliquam nemo ratione enim adipisci. Accusantium labore ex velit. Qui a vel dignissimos ipsa. Georgiana Far Morganica

glory hole girlz · 2021年3月5日 上午11:23

I very delighted to find this web site on bing, just what I was searching for : D also saved to fav. Berget See Kaete

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注