题解 | 蛇形矩阵
蛇形矩阵
https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
#include <iostream>
#include <vector>
using namespace std;
void f(int n){
vector<vector<int>> vec(n, vector<int>(n, 0));
int now = 1;
for(int k = 0; k < n; k++){
int i = k;
int j = 0;
while(i >= 0){
vec[i][j] = now;
now++;
i--;
j++;
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(vec[i][j] != 0){
cout << vec[i][j] << " ";
}
}
cout << endl;
}
}
int main() {
int n;
cin >> n;
f(n);
//int l[100][100] = {0};
// l[0][0] = 1;
// for(int i = 0; i < n; i++){
// for(int j = 1; j < n; j++){
// int t = i + j + 1;
// l[i][j] = l[i][j - 1] + t;
// }
// if(i < 99){
// l[i + 1][0] = l[i][0] + i + 1;
// }
// }
// int ln = n;
// for(int i = 0; i < n; i++){
// for(int j = 0; j < ln; j++){
// cout << l[i][j] <<" ";
// }
// ln--;
// cout << endl;
// }
}
// 64 位输出请用 printf("%lld")
查看5道真题和解析