输入有多组数据,每组只有一行,包含
x,y(1 <= x,y <= 30)
和
t
,输出
x*y
螺旋方阵,如果
t=0
,就输出逆时针螺旋方阵,否则输出顺时针的
样例输出(每个数字要占四个格子,输出完一组就接着输出一个空行):
1 12 11
2 13 10
3 14 9
4 15 8
5 6 7
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
样例输出(每个数字要占四个格子,输出完一组就接着输出一个空行):
1 12 11
2 13 10
3 14 9
4 15 8
5 6 7
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
#include
<iostream>
#include
<iomanip>
using
namespace std;
int
main()
{
int M,N;
cout << "
输入每行个数N:"
;
cin >> N;
cout << "
输入每列个数M:"
;
cin >> M;
int up = 0; //
上边界
int down = M-1; //
下边界
int left = 0; //
左边界
int right = N-1; //
右边界
int num = 1; //
从一开始累加
int array[20][20];
int t;
cout << "0-----
逆时针输出:/n其他--顺时针输出/n输入:"
;
cin >> t;
if (t != 0)
{
for (;up <= down, left <= right; up ++, down --, left ++, right --)
{
for (int i = left; i <= right; i ++) array[up][i] = num ++;
for (i = up + 1; i < down; i ++) array[i][right] = num ++;
for (i = right ; i >= left; i --) array[down][i] = num ++;
for (i = down-1 ; i > up; i --) array[i][left] = num ++;
}
}
else if (t == 0)
{
for (;up < down, left <= right; up++, down --, left ++, right --)
{
for (int i = up; i <= down; i ++) array[i][left] = num++;
for (i = left + 1; i < right; i ++) array[down][i] = num++;
for (i = down; i >= up; i --) array[i][right] = num ++;
for (i = right - 1; i >left; i --) array[up][i] = num ++;
}
}
for (int i = 0; i < M; i ++)
{
for (int j = 0; j < N; j ++)
cout << setw(4) << array[i][j];
cout << endl;
}
return 0;
}
|