题解 | #蛇形矩阵#
蛇形矩阵
https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
#include <iostream> using namespace std; int main() { int v[100][100], n, a = 1; cin >> n; for (int i = 0; i < n; i++) //循环 i 行 { for (int j = 0; j <= i; j++) //记录第 i 行第 i 列的数据,注意 j<=i { v[i - j][j] = a; //i 递减,j 递增 —— 左下至右上 a++; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n - i; j++) { cout << v[i][j]; if (j != n - i) cout << " "; } cout << endl; } return 0; }