三角螺旋阵

题目描述


方阵的主对角线之上称为“上三角”。
请你设计一个用于填充n阶方阵的上三角区域的程序。填充的规则是:使用1,2,3….的自然数列,从左上角开始,按照顺时针方向螺旋填充。

输入

程序运行时,从标准输入获得整数n(3~20)

输出

程序输出:方阵的上三角部分。
要求格式:每个数据宽度为4,右对齐。

样例输入

3

样例输出

   1   2   3
   6   4
   5

#include
#include
using namespace std;
int main()
{int n;
cin>>n;
int i,s,t,h,g,m=1,b,c,a[50][50],d=0,e=0,f=0,z=0,x=0;
b=c=n-2;
g=n;
for(i=0;i
{ for(t=i;t
{a[i][t]=m;
m++;}
f++;
  g--;
   for(h=i+1;h
   {a[h][t-2]=m;
   m++;
   t--;
   }
   z++;
   g--;

    for(c=b;c>n-2-g-x;c--)
 {a[h-2][e]=m;
 m++;
 h--;}
 g--;
 e++;
 b--;
 x++;
}

 for(i=0;i
  for(s=0;s
  {m=a[i][s];
  cout<<setw(4)<<m;
  if(s==n-1-i)
   cout<<endl;
  }
  return 0;
  
}

原文地址:https://www.cnblogs.com/oversea201405/p/3767070.html