1 回答

TA貢獻(xiàn)24條經(jīng)驗(yàn) 獲得超7個(gè)贊
#include <iostream>
using namespace std;
int main()
{
int **array;
int i, j, k, n, x = 1, y = -1, flag = 0;
cin >> n;
array = new int*[n];
for(i = 0; i < n; i++)
array[i] = new int[n];
for(i = 0, j = 0, k = 0; i < n * n; i++)
{
if(j < 0)
{
j = 0;
flag = 1;
x = 1;
y = -1;
}
else if(j >= n)
{
if(k < 0)
k = 1;
else
k += 2;
j = n - 1;
y = 1;
x = -1;
}
if(k < 0)
{
k = 0;
x = -1;
y = 1;
}
else if(k >= n)
{
if(flag)
j++;
else
j += 2;?
k = n - 1;
x = 1;
y = -1;
}
array[j][k] = i + 1;
j += x; //選擇行縮進(jìn)方式
k += y; //選擇列縮進(jìn)方式
flag = 0;
}
for(j = 0; j < n; j++)
{
for(k = 0; k < n; k++)
cout << array[j][k] << '\t';
cout << endl;
}
return 0;
}
- 1 回答
- 1 關(guān)注
- 1397 瀏覽
添加回答
舉報(bào)