打印某个图形

banmiton 2007-06-04 09:32:40
先由用户输入一个数 N
由 N 的大小来决定输出的图形
举个例子:当 N 等于5时,
输出的图形是一个 5*5 的矩阵,图形按顺时针方向一直向内旋转,直至结束.
打印图形如下:
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
如何输出这样的图形?
...全文
157 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
banmiton 2007-06-04
  • 打赏
  • 举报
回复
谢谢大家。。。。那我去搜索了。。
jixingzhong 2007-06-04
  • 打赏
  • 举报
回复
【ref】

/* The following can be compiled and executed in
* visual C++ 6.0 sp6 */

#include "stdio.h"
#define MAX_AM 100

int helixMatrix[MAX_AM][MAX_AM]; // global variable

/*
*(mt_x,mt_y): coordinate of the 1st element
* start:the value of 1st element
* n: the size of the helixMatrix
*/
void SethelixMatrix(int mt_x, int mt_y, int start, int n)
{
int i, j;

if (n <= 0) //the qualification of end for the recursion
return;
if (n == 1)
{ //the extreme size of helixMatrix : 1
helixMatrix[mt_x][mt_y] = start;
return;
}

for (i = mt_x; i < mt_x + n-1; i++) //the top of the matrix
{
helixMatrix[mt_y][i] = start++;
}

for (j = mt_y; j < mt_y + n-1; j++) // the right of it
{
helixMatrix[j][mt_x+n-1] = start++;
}

for (i = mt_x+n-1; i > mt_x; i--) //the bottom of it
{
helixMatrix[mt_y+n-1][i] = start++;
}

for (j = mt_y+n-1; j > mt_y; j--) //the left of it
{
helixMatrix[j][mt_x] = start++;
}

SethelixMatrix(mt_x+1, mt_y+1, start, n-2); // the recursion
}

void main()
{
int i, j;
int n;

printf(" Input the size you want");
printf("\n\t");
scanf("%d", &n);
SethelixMatrix(0, 0, 1, n);

//print the helixMatrix
for(i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
printf("%4d", helixMatrix[i][j]);
printf("\n");
}
}

//螺旋matrix
jixingzhong 2007-06-04
  • 打赏
  • 举报
回复
搜索一下 螺旋矩阵 ...
jixingzhong 2007-06-04
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#define N 5 //用宏控制
int main(int argc, char *argv[])
{
int i,j,n=1,a[N][N];
for(i=0;i<=N/2;i++)
{
for(j=i;j<N-i;j++)
a[i][j]=n++;

for(j=i+1;j<N-i;j++)
a[j][N-i-1]=n++;

for(j=N-i-2;j>i;j--)
a[N-i-1][j]=n++;

for(j=N-i-1;j>i;j--)
a[j][i]=n++;
}

for(i=0;i<N;i++)
{
printf("\n\n");
for(j=0;j<N;j++)
printf("%5d",a[i][j]);
}

system("pause");
return 0;
}
bargio_susie 2007-06-04
  • 打赏
  • 举报
回复
搜索螺旋距阵

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧