在螺旋方向上以二维阵列存储单维阵列? [英] To store a single dimensional array in a two-dimensional array in spiral direction?.

查看:87
本文介绍了在螺旋方向上以二维阵列存储单维阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Example:
Input array: 1 2 3 4 5 6 7 8 9 10 11 12 13
Matrix size: 4 4
Output:
 1  2 3  4
12 13 0  5
11 0  0  6
10 9  8  7
Note: remaining elements in the matrix should be filled with zeroes
....



我试过的是:

我只是为每个方向使用了四个循环然后分配给了2d中的特定元素阵列。我找不到简单的逻辑。请任何人告诉任何逻辑以简单的方式填充螺旋方向的元素。我之前试过发布这个问题。有人删除了我的帖子。请不要删除我的问题,以便其他人可以帮助我。

等待回答。

..

看不到我的解决方案..它的复杂性。它更好地给出你自己的逻辑。提前致谢。




What i have tried is:
I just used four loops for each direction and then assigned to specific element in a 2d array. i could not find easy logic. please any one tell any logic to fill the elements in spiral direction in simple way. i tried to post this question before. someone removed my post. kindly dont remove my question so that others may help me.,
awaiting for answer.
..
dont see my solution.. its complex . its better give your own logic. thanks in advance.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m,q1,n,p,k=0,a[100],b[10][10],q,x=0,y=0;
clrscr();
printf("\n enter the size of the array:");
scanf("%d",&p);
printf("\n enter the array");
for(i=0;i<p;i++)>
{
scanf("%d",&a[i]);
}
printf("\n enter the size of the 2d array");
scanf("%d%d",&m,&n);
q=n;q1=m;
for(i=0;i<m;i++)>
{
for(j=0;j<n;j++)>
{
b[i][j]=0;
}
}
/*for(i=0;i<m;i++)>
{
for(j=0;j<n;j++)>
{
printf("%d\t",b[i][j]);
}
printf("\n");
}*/
while(x<m&&y><n)>
{
for(j=y;((j<n)&&(k><p));j++)>
{
b[x][j]=a[k];
k++;
}
x++;
for(j=x;((j<m)&&(k><p));j++)>
{
b[j][n-1]=a[k];
//printf("%d",b[j][n-1]);
k++;
}
n--;
if(x<m)>
{
for(j=n-1;((j>=y)&&(k<p));j--)>
{
b[m-1][j]=a[k];
//printf("\n %d",b[n][j]);
k++;
}
m--;
}
if(y<n)>
{
for(j=m-1;((j>x-1)&&(k<p));j--)>
{
b[j][y]=a[k];
k++;
}
y++;
}
//do
//{
//i=i+1;
/*for(i=i+1;i<m;i++)>
{
for(j=i;((j<n)&&(k><p));j++)>
{
if(b[i][j]==0)
{
b[i][j]=a[k];
k++;
}
}*/
}//while(b[i][j]==0);
for(i=0;i<q1;i++)>
{
for(j=0;j<q;j++)>
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
getch();
}

推荐答案

我只有一对索引(x和y)和一对加法器( dx和dy),可能带有一对限制索引(lx和ly)



加法器每个都可以具有值-1,0或1控制方向。



预设x = 0,y = 0,dx = 1,dy = 0,lx = 4,ly = 0

遍历1D数组,在(x,y)处插入数据,然后添加加法器(dx,dy)。如果在极限处检查加法器并旋转90度 - 那么(dx = 1,dy = 0)变为(dx = 0; dy = 1),依此类推。设置下一个限制。



手动尝试,你会明白我的意思。

这种方法的优点是这是一个单循环,它适用于任何大小的2D数组,前提是你有正确数量的输入元素。
I would just have a pair of indexes (x and y) and a pair of "adders" (dx and dy), possibly with a pair of "limit" indexes (lx and ly)

The adders can each have the value -1, 0, or 1 to control the direction.

Preset x = 0, y = 0, dx = 1, dy = 0, lx = 4, ly = 0
Loop through the 1D array, inserting the data at (x,y), then add the adders (dx, dy). If at the limit check the adders and "rotate 90 degrees" - so (dx = 1, dy = 0) becomes (dx = 0; dy = 1) and so on. Set up the next limits.

Try it by hand, and you'll see what I mean.
The advantage of this approach is that it's a single loop, and it works for any size of 2D array, provided you have the right number of input elements.


这篇关于在螺旋方向上以二维阵列存储单维阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆