- 蛇形填数2
60分,想知道问题在哪
- 2024-4-4 22:11:59 @
```
#include<iostream>
#include<vector>
using namespace std;
int main() {
int m, n;
cin >> m >> n;
vector<vector<int>> nums(m+1, vector<int>(n+1, 0));
int top = 1, bottom = m, left = 1, right = n;
for (int i = 1;i<=m*n;) {
for (int j = top; j <= bottom; ++j) {
nums[j][right] = i;
i++;
}
right--;
for (int j = right; j >= left; --j) {
nums[bottom][j] = i;
i++;
}
bottom--;
for (int j = bottom; j >= top; j--) {
nums[j][left] = i;
i++;
}
left++;
for (int j = left; j <= right; ++j) {
nums[top][j] = i;
i++;
}
top++;
}
for(int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
printf("%5d", nums[i][j]);
}
cout << endl;
}
return 0;
}
1 条评论
-
qzhs LV 2 MOD @ 2024-4-5 2:35:51
#include<iostream> #include<vector> using namespace std; int main() { int m, n; cin >> m >> n; vector<vector<int>> nums(m+1, vector<int>(n+1, 0)); int top = 1, bottom = m, left = 1, right = n; for (int i = 1;i<=m*n;) { for (int j = top; j <= bottom; ++j) { if(i > m*n) {//边界跳出 break; } nums[j][right] = i; i++; } right--; for (int j = right; j >= left; --j) { if(i > m*n) { break; } nums[bottom][j] = i; i++; } bottom--; for (int j = bottom; j >= top; j--) { if(i > m*n) { break; } nums[j][left] = i; i++; } left++; for (int j = left; j <= right; ++j) { if(i > m*n) { break; } nums[top][j] = i; i++; } top++; } for(int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { printf("%5d", nums[i][j]); } cout << endl; } return 0; }
- 1
信息
- ID
- 78
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 7
- 标签
- 递交数
- 682
- 已通过
- 175
- 上传者