题解 | #蛇形矩阵#
蛇形矩阵
http://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
矩阵m[i][j]=m[i][j-1]+j+i-1
#include <algorithm>
using namespace std;
int main() {
int n;
while(cin>>n){
int rowindex = 1;
int colindex = 1;
for(int i=1;i<=n;i++){//一行一行的输出,
cout<<rowindex<<' ';
int j =2;
colindex = rowindex;
while(j<=n-i+1){
colindex+=j+i-1;
cout<<colindex<<' ';
j++;
}
cout<<endl;
rowindex+=i;
}
}
}