效果图如下:
代码如下:
#include "iostream" using namespace std; const int MAXSIZE = 10; int array[MAXSIZE+1][MAXSIZE+1]={0}; void assist(int); void deal(int row,int column,int& num,int n); int main() { int n,i,j,num=1,column=1,row=1,cnt=0; cout << "请输入阶数:" << endl; cin >> n; assist(n); while(cnt!=(n%2==0? n/2 : n/2 + 1)) { deal(row,column,num,n);row++;column++;cnt++; } for(i=1;i<=n;++i) { for(j=1;i+j<n+2;++j) cout << array[i][j] << "\t"; cout << endl; } } void assist(int n) { for(int i=0;i<=n+2;++i)//建围墙 { array[i][0] = array[0][i] = 1; for(int j=1;j<=n+2;++j) (i+j==n+2? array[i][j]=1 : array[i][j]=0); } } void deal(int row,int column,int& num,int n) { int i=row,j,tmp; for(j=column;!array[i][j];++j)//→ { array[i][j] = num++; } tmp = i+j-1; for(i=row+1;;++i)//斜着 {j=tmp-i; if(array[i][j]==0)array[i][j]=num++; else break; } j++;i-=2; for(;!array[i][j];i--) { array[i][j]=num++; } }
0.0分
0 人评分