输出这样的三角形 1 2 3 4 5 12 13 14 6 11 15 7 10 8 9

问题描述:

输出这样的三角形 1 2 3 4 5 12 13 14 6 11 15 7 10 8 9
1 2 3 4 5
12 13 14 6
11 15 7
10 8
9

#include
using namespace std;
void print(int b)
{
int orientation = 0;
int** a = new int*[b+1];
for(int i = 0; i {
a[i] = new int[b+1];
}
for(int i = 0; i {
for(int j= 0; j {
if(i+j {
a[i][j] = 0;
}
else
{
a[i][j] = -1;
}
}
}
a[0][0] = 1;
int x = 0;
int y = 0;
int num = (1+b)*b/2;
for(int i = 2; i = 1 && a[x-1][y+1] == 0)
{
x -= 1;
y += 1;
a[x][y] = i;
continue;
}
orientation = 2;
}
if(orientation == 2)
{
if(a[x][y-1] == 0)
{
y -= 1;
a[x][y] = i;
continue;
}
orientation = 0;
}
if(orientation == 0)
{
if(a[x+1][y] == 0)
{
x += 1;
a[x][y] = i;
continue;
}
orientation = 1;
}
}
for(int i = 0; i {
for(int j = 0; j {
cout