打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
螺旋矩阵输出
#include <stdio.h>
void Output(int n);
void main(void)
{
 int n = 0 ;
 scanf("%d",&n);
 if(n<=0)
  return;
 Output(n);
}
void WriteToBuf(bool flag,int loop,int *x,int *y,int *pmatrix,int maxn)//将第loop圈的数据写入数组
{
 int init = (loop-1)*(loop-1)+1;
 int loopnum = loop*loop-init+1;
 int tempx = *x;
 int tempy = *y;
 int tempnum = init;
 if(flag)
 {
  tempx+=1;
  pmatrix[tempy*maxn+tempx] = tempnum;  
  for(int i = 0;i<loopnum-1;i++)
  {
   tempnum++;
   if(i<loop-1)
    tempy++;
   else
    tempx--; 
   pmatrix[tempy*maxn+tempx] = tempnum;
  }
 }
 else
 {
  tempx-=1;
  pmatrix[tempy*maxn+tempx] = tempnum;  
  for(int i = 0;i<loopnum-1;i++)
  {
   tempnum++;
   if(i<loop-1)
    tempy--;
   else
    tempx++; 
   pmatrix[tempy*maxn+tempx] = tempnum;
  }
 }
 *x = tempx;
 *y = tempy;
}
void Output(int n)
{
 int *pMatrix = new int [n*n];
 bool flag = 1;
 int onex = (n-1)/2;
 int oney = (n-1)/2;
 
 int tempx = onex;
 int tempy = oney;
 pMatrix[tempy*n+tempx] = 1;
 for(int i = 2;i<=n;i++)//循环将每一圈数据写入数组
 {
  WriteToBuf(flag,i,&tempx,&tempy,pMatrix,n);
  flag = !flag;
 }
 for(i = 0;i<n*n;i++)
 {
  if((i)%n==0)
  printf("\n");
  printf("%d ",pMatrix[i]);
 }
 printf("\n");
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
最小二乘曲线拟合
边框收藏
触摸屏控制方法(转)
luogu P3387 【模板】缩点
贪吃蛇源程序
八皇后问题 --回溯 解析
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服