题解 | #蛇形矩阵#
蛇形矩阵
https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
#include <iostream> #include <vector> using std::cin; using std::cout; using std::endl; using std::vector; int main(int argc, char* argv[]) { vector<vector<int>> vec(100,vector<int>(100)); // 初始化第一列 vec[0][0] = 1; for (int i = 1; i < 100; i++) { vec[i][0] = vec[i - 1][0] + i; } //基于第一列初始化所有行 for (int j = 0; j < 100; j++) { for (int k = 1; k < 100; k++) { vec[j][k] = vec[j][k - 1] + k + j + 1; } } int num = 0; cin >> num; int num1 = num; // 输出蛇形矩阵 for (int i = 0; i < num; i++) { for (int j = 0; j < num1; j++) { cout << vec[i][j] << " "; } num1--; cout << endl; } return 0; }