2 条题解
-
0
更新一下码风,发一篇
草履虫的做法题解#include <iostream> using namespace std; int n, m, arr[100][100], i, l, r, u, d; int main() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> n >> m; for (i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> arr[i][j]; l = u = 0, r = m - 1, d = n - 1; while (1) { for (i = l; i <= r; ++i) cout << arr[u][i] << ' '; if (++u > d) break; for (i = u; i <= d; ++i) cout << arr[i][r] << ' '; if (l > --r) break; for (i = r; i >= l; --i) cout << arr[d][i] << ' '; if (u > --d) break; for (i = d; i >= u; --i) cout << arr[i][l] << ' '; if (++l > r) break; } }
假的,我有更优雅的写法
#include <iostream> using namespace std; int n, m, arr[100][100], l, i, j, f, dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; bool vis[100][100]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> n >> m; for (i = 0; i < n; ++i) for (j = 0; j < m; ++j) cin >> arr[i][j]; l = n * m; i = j = f = 0; for (int _ = 0, ti, tj; _ < l; ++_) { cout << arr[i][j] << ' ', vis[i][j] = 1; ti = i + dir[f][0], tj = j + dir[f][1]; if (ti < 0 || ti >= n || tj < 0 || tj >= m || vis[ti][tj]) f = (f + 1) % 4; i += dir[f][0], j += dir[f][1]; } }
信息
- ID
- 129
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 6
- 标签
- (无)
- 递交数
- 103
- 已通过
- 29
- 上传者