题解 | #蛇形矩阵#
蛇形矩阵
https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
#include <iostream> #include <vector> using namespace std; int main() { int N; cin >> N; vector<vector<int>> res(N, vector<int>(N)); int num = 1; for (int i = 0; i < N; i++) { int j = i; for (int k = 0; j >= 0 && k < N; k++, j--) { res[j][k] = num; num++; } } for (int i = 0; i < N; i++) { for (int j = 0; j < N - i; j++) { cout << res[i][j] << " "; } cout << endl; } } // 64 位输出请用 printf("%lld")
蛇形遍历
#华为机试#华为OD机测试题 文章被收录于专栏
个人练习专栏