# 给定整数N，按要求输出Z字型矩阵

``````#include<iostream>
using namespace std;
void display(int N)
{
int*a=new int[N];//先声明一个数组
memset(a,0,N*sizeof(int));
if(N%2==0)
{
for(int i=1;i<=N;i++)
{//先打印第一行
if(i%2==1)
{//第一行中奇数序列的数
for (int j = 1; j <=i; j++)
{
a[i-1]+=j;
}
}
else
{
a[i-1]=a[i-2]+1;
}

cout<<a[i-1]<<"  ";

}
cout<<endl;
for(int k=2;k<=N;k++)
{
if(k%2==0)
{
for (int i = 1; i <=N-1; i++)
{
if(i%2==1)
{
a[i-1]=a[i]+1;
}else
{
a[i-1]=a[i]-1;
}
cout<<a[i-1]<<"  ";
}

a[N-1]=a[N-1]+2*(N-k+1);

cout<<a[N-1];
}
else
{
for (i = 1; i <=N-1; i++)
{
if(i%2==1)
{
a[i-1]=a[i]-1;
}
else
{
a[i-1]=a[i]+1;
}
cout<<a[i-1]<<"  ";
}
a[N-1]=a[N-1]+1;
cout<<a[N-1];
}
cout<<endl;;
}
}
else
{
for(int i=1;i<=N;i++)
{//先打印第一行
if(i%2==1)
{//第一行中奇数序列的数
for (int j = 1; j <=i; j++)
{
a[i-1]+=j;
}
}
else
{
a[i-1]=a[i-2]+1;
}

cout<<a[i-1]<<"  ";

}
cout<<endl;
for(int k=2;k<=N;k++)
{
if(k%2==1)
{
for (int i = 1; i <=N-1; i++)
{
if(i%2==0)
{
a[i-1]=a[i]+1;
}else
{
a[i-1]=a[i]-1;
}
cout<<a[i-1]<<"  ";
}

a[N-1]=a[N-1]+2*(N-k+1);

cout<<a[N-1];
}
else
{
for (i = 1; i <=N-1; i++)
{
if(i%2==0)
{
a[i-1]=a[i]-1;
}
else
{
a[i-1]=a[i]+1;
}
cout<<a[i-1]<<"  ";
}
a[N-1]=a[N-1]+1;
cout<<a[N-1];
}
cout<<endl;;
}

}
delete []a;
}
int main()
{
int n;
cin>>n;
display(n);
return 0;
}``````
原文作者：Z字形编排问题
原文地址: https://blog.csdn.net/u014082714/article/details/44974535
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。