题目描述
输入两个整数 n 和 m,输出一个 n 行 m 列的矩阵,将数字 1 到 n×m 按照回字蛇形填充至矩阵中。
具体矩阵形式可参考样例。
样例
输入样例:
3 3
输出样例:
1 2 3
8 9 4
7 6 5
算法1
淦!不想写啥了,直接上代码
include<----->//淦,想用万能头的,题解打不出来啊
using namespace std;
const int N = 105;
int a[N][N];
int n,m;
int main()
{
cin>>n>>m;
int left=0,right=m-1,top=0,bottom=n-1;
int k=1;
while (left<=right&&top<=bottom)
{
for(int i=left;i<=right;i)
{
a[top][i]=k;
}
for(int i=top+1;i<=bottom;i)
{
a[i][right]=k;
}
for(int i=right-1;i>=left&&top[HTML_REMOVED]top&&left<right;i–)
{
a[i][left]=k;
}
left,right–,top,bottom–;
}
for(int i=0;i<n;i)
{
for(int j=0;j<m;j++)
{
cout << a[i][j] << ” “;
}
cout << endl;
}
return 0;
}
牛
牛