问题描述:
代码示例:
cpp
//蛇形矩阵
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cout << "输入样例" << endl;
cin >> n;
int k = 1;
for(int i = 0; i < n; i++){
if( i %2 == 0){
//单数行
for(int j = 0; j < n; j++){
cout << k++ << " ";
}
} else{
//双数行,逆置
for(int j = k+n-1; j >= k; j--){
cout << j << " ";
}
k += n;//保证k在下一行的初始值不出错
}
cout << endl;
}
return 0;
}
运行结果: