螺旋方阵问题

BUCK650 2012-04-11 09:46:17
螺旋方阵存放在n*n的二维数组中并将其打印输出,要求n由程序读入,数字螺旋方阵由程序自动生成(而非人为的初始化或逐个输入)。
例如: 1 16 15 14 13
2 17 24 23 12
3 18 25 22 11
4 19 20 21 10
5 6 7 8 9
...全文
799 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenxue0821 2013-10-09
  • 打赏
  • 举报
回复
int main(){
	const int n = 5;
	int arr[n][n] = {0};

	int direction[4][2] = {{1, 0},		//right
				{0, 1},		//down
				{-1, 0},	//left
				{0, -1}};	//up

	int dx = 1, dy = 0, x = 0, y = 0, dir = 0;
	for(int i = 0; i < n * n; i++, x += dx, y += dy){
		arr[x][y] = i + 1;
		if(x + dx == -1 || y + dy == -1 || x + dx == n || y + dy == n
				|| arr[x + dx][y + dy] != 0){
			dir = (dir + 1) % 4;
			dx = direction[dir][0];
			dy = direction[dir][1];
		}
	}

	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++)
			printf("%2d ", arr[i][j]);
		printf("\n");
	}

}
lin5161678 2013-10-09
  • 打赏
  • 举报
回复
自动运行的贪吃蛇 撞墙 或者 遇到已经走过的 就转弯 而已
babycool252 2013-10-09
  • 打赏
  • 举报
回复
public class SpinLongPrinter { public static final int SIZE=10; private static int[][] matrix; private int size; public SpinLongPrinter(int size) { this.size=size; matrix=new int[size][size]; } public static void main(String[] args) { SpinLongPrinter slp = new SpinLongPrinter(SIZE); slp.initMatrix(SIZE, 1); slp.print(); } private void initMatrix(int seqNum,int beginNum){ int last=getLast(seqNum, beginNum); int temp=SIZE/2-seqNum/2; for(int i=temp;i<temp+seqNum;i++){ if(i==temp){ for(int j=temp;j<temp+seqNum;j++){ matrix[i][j]=beginNum+j-temp; } } else if(i==temp+seqNum-1){ int beginLast=last-seqNum+2; for(int j=temp;j<temp+seqNum;j++){ matrix[i][j]=beginLast-j+(temp); } } else{ matrix[i][temp]=last-i+temp+1; matrix[i][temp+seqNum-1]=matrix[i-1][temp+seqNum-1]+1; } } if(seqNum!=1&&seqNum!=2) initMatrix(seqNum-2, last+1); } private void print(){ for(int i=0;i<size;i++){ for(int j=0;j<size;j++){ if(matrix[i][j]<10) System.out.print(" "+matrix[i][j]+" "); else System.out.print(matrix[i][j]+" "); } System.out.println(); } } private static int getLast(int n,int m){ return m+n*4-4-1; } } 不是一个一个转圈的 而是从外到内
赵4老师 2012-08-22
  • 打赏
  • 举报
回复
KISS - Keep It Simple and Stupid!
#include <stdio.h>
#define MAXN 10
char m[MAXN+2][MAXN+2];
char d;
int x,y,k,n;
void main() {
while (1) {
printf("Input n(1..%d):",MAXN);
fflush(stdout);
rewind(stdin);
if (1==scanf("%d",&n)) {
if (1<=n && n<=MAXN) break;
}
}
y=0 ;for (x=0;x<=n+1;x++) m[y][x]=1;
y=n+1;for (x=0;x<=n+1;x++) m[y][x]=1;
x=0 ;for (y=0;y<=n+1;y++) m[y][x]=1;
x=n+1;for (y=0;y<=n+1;y++) m[y][x]=1;
for (y=1;y<=n;y++) {
for (x=1;x<=n;x++) {
m[y][x]=0;
}
}
x=1;
y=1;
k=0;
d='D';
while (1) {
k++;
if (k>n*n) break;
m[y][x]=k;
switch (d) {
case 'D':
if (0==m[y+1][x]) y++;
else {x++;d='R';}
break;
case 'R':
if (0==m[y][x+1]) x++;
else {y--;d='U';}
break;
case 'U':
if (0==m[y-1][x]) y--;
else {x--;d='L';}
break;
case 'L':
if (0==m[y][x-1]) x--;
else {y++;d='D';}
break;
}
}
for (y=1;y<=n;y++) {
for (x=1;x<=n;x++) {
printf("%4d",m[y][x]);
}
printf("\n");
}
}

Cathleenyu922 2012-08-22
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
int a[101][101];

int main(void)
{
int n;
int i1, i2, j1, j2, j, num;
while(scanf("%d", &n))
{
num=1;
j1=1;
j2=n;
for(i1=1,i2=n;i1<=i2;i1++,i2--)
{
for(j=j1;j<=j2;j++)
a[i1][j]=num++;
for(j=i1+1;j<=i2;j++)
a[j][j2]=num++;
j2--;
for(j=j2;j>=j1;j--)
a[i2][j]=num++;
for(j=i2-1;j>i1;j--)
a[j][j1]=num++;
j1++;
}
for(i1=1;i1<=n;i1++)
{
for(j=1;j<=n;j++)
{
printf("%d ", a[i1][j]);
}
printf("\n");
}
}
return 0;
}
imlt1234 2012-04-17
  • 打赏
  • 举报
回复
#include<stdio.h>
//#define N 5
int abs(int x)
{
if(x>0)
return x;
else
return -x;
}
int getNum(int x,int y)
{
int n,m; //n 強嵼殶悢丆m 忋殶揑枛旜
int val; //val
if(x==0 && y== 0)
{
return 1;
}

n=abs(x)>abs(y)?abs(x):abs(y);
m=(2*n-1)*(2*n-1);
if(x==n && y!=-n)
{
val=n+m+y;
}
if(y==n && x!=n)
{
val=m+n-x+2*n;
}
if(x==(-n) && y !=n)
{
val=m+n-y+2*n*2;
}
if(y==-n && x!= -n)
{
val=m+n+x+2*n*3;
}
return val;

}
void main()
{
int i ,j;
int num;
for( i=-1;i<=1;i++)
{
for( j=-1;j<=1;j++)
{
num=getNum(j,i);
printf("%d",num);
printf("\t");
}
printf("\n");
}

}
SATANZL 2012-04-17
  • 打赏
  • 举报
回复
#include "stdio.h"
main()
{
int a[200][200]={0};
int i=1,j,k=1,t,s,x,y;
scanf("%d",&s);
int i1=1,j1=1,x1=s-1,y1=s-1;
int i2=s,j2=s,x2=2,y2=1;
for(;k<=s*s;j1++,i1++,j2--,i2--,
y1--,y2++,x1--,x2++)
{
for(j=j1,i=i1;j<=j2;j++) //???????§Ú??
{
a[i][j]=k++;
}
j--;
for(i=i1+1;i<=i2;i++) //???????§Ú??
{
a[i][j]=k++;
}
for(y=y1,x=x1+1;y>=y2;y--) //?????§Ú??
{
a[x][y]=k++;
}
y++;
for(x=x1;x>=x2;x--) //????§Ú??
{
a[x][y]=k++;
}
}
for(i=1;i<=s;i++)
for(j=1;j<=s;j++)
{
printf((j)%s==0?" %3d\n":" %3d",a[i][j]);
}
}之前写的,可能有点乱
goldbeef 2012-04-16
  • 打赏
  • 举报
回复
LZ的结贴率啊
PANHL97 2012-04-16
  • 打赏
  • 举报
回复

void main()
{
int n,i,max;
int row,col;
int *mat;
int flag;

printf("please enter the edge:");
scanf("%d",&n);
flag=0;
max=n*n;
row=0;col=0;
mat=(int*)malloc(sizeof(int)*max);

for(i=0;i<max;i++)
{
mat[row*n+col]=i+1;
switch(flag)
{
case 0:
row++;
if(n-row-col==1)
flag=1;
break;
case 1:
col++;
if(row==col)
flag=2;
break;
case 2:
row--;
if(n-row-col==1)
flag=3;
break;
case 3:
col--;
if((col-1)==row)
flag=0;
break;
}
}
for(i=0;i<max;i++)
{
printf("%4d ",mat[i]);
if((i+1)%n==0)
printf("\n");
}
system("pause");
}
鱼珊珊 2012-04-16
  • 打赏
  • 举报
回复
#include<stdio.h>
#define N 6
int main()
{
int i,j,t=0,k,m=0,sum=1;
int a[N][N];
for(i=0;i<N/2;i++,m+=2)//层次
{
for(k=1;k<N-m;k++)//左
{
a[k-1+i][i]=sum;
sum+=1;
}
for(k=1;k<N-m;k++)//下
{
a[N-1-i][k-1+i]=sum;
sum+=1;
}
for(k=1;k<N-m;k++)//右
{
a[N-k-i][N-1-i]=sum;
sum+=1;
}
for(k=1;k<N-m;k++)//上
{
a[i][N-k-i]=sum;
sum+=1;
}
}
if(N%2!=0)
a[N/2][N/2]=N*N;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
printf("%3d",a[i][j]);
if(j==N-1)
printf("\n");
}
return 0;
}
goldbeef 2012-04-16
  • 打赏
  • 举报
回复
#include <iostream>
#define MaxEdge 10
using namespace std;

void f(int start_edge)
{
int Matrix[MaxEdge][MaxEdge];
int curt_edge;
curt_edge=start_edge;
int max_value=start_edge*start_edge;
int cur_row=1,cur_col=1;
int cur_value=1;
while (curt_edge>0)
{
int ctr_right=curt_edge;
int ctr_down=curt_edge-1;
int ctr_left=curt_edge-1;
int ctr_up=curt_edge-2;
//write right side
while (ctr_right>0)
{
Matrix[cur_row][cur_col]=cur_value;
cur_col++;
cur_value++;
ctr_right--;
}
//chage to down
cur_row++;
cur_col--;
//write down side
while (ctr_down>0)
{
Matrix[cur_row][cur_col]=cur_value;
cur_row++;
cur_value++;
ctr_down--;
}
//chage to left
cur_col--;
cur_row--;
//write left side
while (ctr_left>0)
{
Matrix[cur_row][cur_col]=cur_value;
cur_col--;
cur_value++;
ctr_left--;
}
//chage to up
cur_row--;
cur_col++;
//write up side
while (ctr_up>0)
{
Matrix[cur_row][cur_col]=cur_value;
cur_row--;
cur_value++;
ctr_up--;
}
//chage to right
cur_col++;
cur_row++;
if (cur_value>max_value)
{
break;
}

curt_edge-=2;
}
//show matrix
int i,j;
for (i=1;i<=start_edge;i++)
{
for (j=1;j<=start_edge;j++)
{
cout<<Matrix[i][j]<<" ";
}
cout<<endl;
}
}

int main()
{
int start_edge;
cin>>start_edge;
while (start_edge>0)
{
f(start_edge);
cin>>start_edge;
}
return 0;
}
miracle900 2012-04-15
  • 打赏
  • 举报
回复
晕了………………光一个指针 学的头大…………………………还有链表 完了完了~~~~~~
miracle900 2012-04-15
  • 打赏
  • 举报
回复
额 什么时候才能跨过菜鸟这级别呢……………………
zwb8848happy 2012-04-14
  • 打赏
  • 举报
回复
给你一个原来写过的:

#include <stdio.h>
#include <string.h>

int a[20][20];

void Translate(int n);
void Output(int n);

int main()
{
int n;
while (printf("Please Input a Num: "),scanf("%d",&n)!=EOF)
{
Translate(n);
Output(n);
printf("\n");
}

return 0;
}

void Translate(int n)
{
int i,j,count=0;

memset(a,0,sizeof(a));
a[i=0][j=0]=++count;

while(count<n*n)
{
while (j+1<n&&a[i][j+1]==0)
{
a[i][++j]=++count;
}
while (i+1<n&&a[i+1][j]==0)
{
a[++i][j]=++count;
}
while (j-1>=0&&a[i][j-1]==0)
{
a[i][--j]=++count;
}
while(i-1>=0&&a[i-1][j]==0)
{
a[--i][j]=++count;
}
}
}

void Output(int n)
{
int i,j;
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}
}


我们的原则是:

先判断,在移动,而不是走一步以后发现越界了再退回来。
这样我们需要预判,即是否越界,以及如果继续往下走下去会不会达到一个已经填过的的格子!
「已注销」 2012-04-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
想到了个未经修改的方法:

C/C++ code
#include<stdio.h>
#include<stdlib.h>

int fun(int * arr,int edge)
{
int i,j,m,n,flag=1; //i为要记录的数字,m为要直走的格数,n是标记直走的次数
n=1;
m=edge;

for(i=1;i<=ed……
[/Quote]
露了,最后好像是要free(arr);的
「已注销」 2012-04-13
  • 打赏
  • 举报
回复
想到了个未经修改的方法:
#include<stdio.h>
#include<stdlib.h>

int fun(int * arr,int edge)
{
int i,j,m,n,flag=1; //i为要记录的数字,m为要直走的格数,n是标记直走的次数
n=1;
m=edge;

for(i=1;i<=edge*edge; n++)
{
if(!(n%2)) m--;

if(n%4==1)
for(j=0; j<m;i++,j++)
{ if(flag)
flag=0;
else arr+=edge;
*arr=i;
}
if(n%4==2)
for(j=0;j<m;i++,j++)
{ arr++; *arr=i;}
if(n%4==3)
for(j=0;j<m;i++,j++)
{ arr-=edge; *arr=i;}
if(n%4==0)
for(j=0;j<m;i++,j++)
{ arr--; *arr=i;}
}
return 0;
}

int main()
{
int i,j,edge,*arr;
printf("please enter the edge:");
scanf("%d",&edge);
arr=(int*)calloc(edge,sizeof(int));

fun(arr,edge);
for(i=0; i<edge; i++)
{
for(j=0; j<edge; j++)
printf("%6d",arr[i*edge+j]);
printf("\n\n");
}
return 0;
}
清竹小雨 2012-04-12
  • 打赏
  • 举报
回复
我也等参考答案
BUCK650 2012-04-11
  • 打赏
  • 举报
回复
急求助!!!!

69,369

社区成员

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

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