题解 | #蛇形矩阵#
蛇形矩阵
http://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
按数字大小123....观察矩阵下标的变化: (0,0),(1,0)(0,1),(2,0)(1,1)(0,2),(3,0)(2,1)(1,2)(0,3)...x+y之和始终等于所在的行数 i , i 从0增加到n-1,第 i 行有 i+1 个数。
#include <iostream>
using namespace std;
int main()
{
int n; cin >> n;
int a = 0;
int m[100][100];
for (int i = 0; i < n; i++)
for (int j = 0; j <= i; j++)
m[i - j][j] = ++a;
for (int i = 0; i < n; i++){
for (int j = 0; j < n - i; j++) {
cout << m[i][j];
cout << ' ';
}
cout << endl;
}
return 0;
}