void PrintLevel(int** matrix,int tR,int tC,int dR,int dC,bool fromUp)
{
if (fromUp)
{
while (tR != dR + 1)
{
printf("%d", matrix[tR++][tC--]);
}
}
else
{
while (dR != tR - 1)
{
printf("%d ", matrix[dR--][dC++]);
}
}
}
void PrintMatrixZigZag(int **matrix, int rows, int columns)
{
int tR = 0;
int tC = 0;
int dR = 0;
int dC = 0;
int endR = rows - 1;
int endC = columns - 1;
bool fromUp = false;//通过此变量的变化来改变斜线打印的方向
while (tR != endR + 1)
{
PrintLevel(matrix, tR, tC, dR, dC, fromUp);
tR = tC == endC ? tR + 1 : tR;
tC = tC == endC ? tC : tC + 1;
dC = dR == endR ? dC + 1 : dC;
dR = dR == endR ? dR : dR + 1;
fromUp = !fromUp;
}
}
转载于:https://blog.51cto.com/10742910/1747158