c++解法如下: #include <iostream> using namespace std; int main() { const int N = 101; int arrary[N][N];/**存储蛇形矩阵*/ int i,j; int n,num,cnt; for(i=0;i<N;i++) { for(j=0;j<N;j++) arrary[i][j]=0; } arrary[0][0]=1; cin >> n; cnt = n-1; num = n*(n+1)*0.5; for(i=0;i<n;i++) { for(j=0;j<n;j++){ if(i+j==cnt) arrary[i][j]=num--; if(i+j==cnt&&j==0) { cnt--; i=0;j=0; } } } for(i=0;i<n;i++) { for(j=0;i+j<=n-1;j++) { if(i+j<=n-1&&i+j+1>n-1) cout << arrary[i][j] ; else cout << arrary[i][j] << " "; } cout << "\n"; } //cout << "Hello world!" << endl; return 0; }
0.0分
2 人评分