AcWing 756. 蛇形矩阵
原题链接
简单
作者:
jingle
,
2021-01-11 17:59:05
,
所有人可见
,
阅读 427
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll;
int aa[110][110];
int main() {
int n, m;
cin >> n >> m;
int p = 1;
int left = 0, right = m - 1, top = 0, bottom = n - 1;
while (left <= right && top <= bottom) {//每一次循环,之后都再次循环,直到最后
for (int i = left; i <= right; i++) aa[top][i] = p++;
for (int i = top + 1; i <= bottom; i++)aa[i][right] = p++;
for (int i = right - 1; i >= left && top < bottom; i--)aa[bottom][i] = p++;
for (int i = bottom - 1; i > top && left < right; i--)aa[i][left] = p++;
left++, top++, right--, bottom--;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << aa[i][j] << " ";
}
cout << endl;
}
}