Chakravyuha?
#include <stdio.h>int main()
{
int a[10][10],n,i,j,d=0,c=1;
scanf("%d",&n);
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
if(i>=n || j>=n)
a[i][j]=-1;
else
a[i][j]=0;
}
for(c=1,i=0,j=0;c<=n*n;c++)
{
if(a[i][j]==0 && d==0)
{
a[i][j]=c;
j++;
}
else if(a[i][j]==0 && d==1)
{
a[i][j]=c;
i++;
}
else if(a[i][j]==0 && d==2)
{
a[i][j]=c;
j--;
}
else if(a[i][j]==0 && d==3)
{
a[i][j]=c;
i--;
}
if(a[i][j]!=0)
{
if(d==0)
{
i=i+1;
j=j-1;
d=1;
}
else if(d==2)
{
i=i-1;
j=j+1;
d=3;
}
else if(d==1)
{
i=i-1;
j=j-1;
d=2;
}
else if(d==3)
{
i=i+1;
j=j+1;
d=0;
}
}
}
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
}
return 0;
}
Spiral
The prime numbers are written in a spiral form staring at (0,0) and moving as shown in the diagram below. The numbersshown on the right column and the bottom row are the column numbers and row numbers respectively (y and x coordinate
frames).
The objective is to find the position (x and y coordinates) of a given prime.
Input Format:
The input consists of multiple lines.
The first line gives the number of primes (N) in this test case.
The next N lines contain one prime in each line.
Output Format:
The output consists of N lines.
Each consists of a space separated pair of integers giving the x and y coordinates of the corresponding prime in the input.
Constraints:
N≤10
Each prime < 1000000
Example 1
Input
2
3
7
Output
1 0
0 1
Explanation
There are 2 primes in this test case (N=2). The primes are 3 and 7. The coordinates of these in the spiral is (1,0) and (0,1).
The output hence has these in space separated form.
Example 2
Input
3
5
11
13
Output
1 1
1
1
1
0
Explanation
There are 3 primes in this test case (N=2). The primes are 5, 11 and 13. The coordinates of these in the spiral is (1,1), (1,1)
and (1,0).
The output hence has these in space separated form.
Program:
#include<stdio.h>int prime[100];
int a[9][10],num,search[100];
void create()
{
int i=4,j=4,n=3,m=5,n1=5,m1=3,k=0,dire=0;
//creating an array size of 9*10 which is shown in program
while(i!=-1)
{
a[i][j]=prime[k];
k++;
if(dire==0)//Right direction set to 0
{
j++;
if(j==m)
{
m++;
dire=1;
}
}
else if(dire==1)//Up direction set to 1
{
i--;
if(i==n)
{
n--;
dire=2;
}
}
else if(dire==2)//Left direction set to 2
{
j--;
if(j==m1)
{
m1--;
dire=3;
}
}
else if(dire==3)//Down direction set to 3
{
i++;
if(i==n1)
{
n1++;
dire=0;
}
}
}
//Serahing the element postion in the Array
for(k=0;k<num;k++)
{
for(i=0;i<9;i++)
{
for(j=0;j<10;j++)
{
if(a[i][j]==search[k])
{
printf("%d %d\n",j-4,4-i);//printing the position as required
}
}
}
}
}
int main()
{
int n, i = 3, count, c,j=0;
n=100;
//Reading Input
scanf("%d",&num);
for(j=0;j<num;j++)
scanf("%d",&search[j]);
//calculating 100 prime numbers and storing into array
j=0;
if ( n >= 1 )
{
prime[j]=2;
j++;
}
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
prime[j]=i;
j++;
count++;
}
i++;
}
//Calling function to search and create the elements
create();
return 0;
}
Not all the test cases are successfully passes
ReplyDeletePlease provide us the inputs of failed cases and output you are expecting. We will get back to you with corrections.
Delete