本文介绍了在螺旋方向上以二维阵列存储单维阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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();
}
推荐答案
这篇关于在螺旋方向上以二维阵列存储单维阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!