Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
3.11/5 (2 votes)
See more:
I am not clearly understand how to solve.

main()
   {
   int a[10],n,i,j,temp1,max,temp[10];
   clrscr();

   scanf("%d",&n);

   for(i=0;i<n;i++)
   scanf("%d",&a[i]);


   for(i=0;i<n;i++)
   {

   temp[1]=a[0];
   temp[2]=a[temp[1]];
   a[temp[1]]=0  ;
   temp[3]=a[temp[2]];
   a[temp[2]]=temp[1];

   temp[4]=a[temp[3] ] ;
   a[temp[3]]=temp[2];
   }

   for(i=0;i<n;i++)
   printf("%d",a[i] )   ;
   getch();
   }


Input: 3

2 0 1

Output
1 2 0

But for more I dont under stand.
like input: 6
4 3 0 5 1 2
Output
2 4 5 1 0 3
Posted
Updated 16-Apr-15 23:42pm
v3
Comments
Herman<T>.Instance 17-Apr-15 2:45am    
Array.Sort() ?
CPallini 17-Apr-15 2:57am    
What is the expected output (from given input)?
ashwani soft 17-Apr-15 5:40am    
for the value of n=3 output i got is correct but for more value of n like 4,5,6 then how code it further like an like input:for(n=6)
4 3 0 5 1 2 then i want this Output 2 4 5 1 0 3
OriginalGriff 17-Apr-15 5:58am    
How?
I may be being thick here, but I can't see any logical reason for the translation from "201" to "120", let alone "430512" to "245103"
Unless we know what the algorithm is, there isn't a lot we can really do to help...
CPallini 17-Apr-15 9:11am    
And what is the logical rule, behind this ordering?

What about this:

C++
#include <stdio.h>
main()
{
  int n;
  scanf("%d",&n);
  int in[n],out[n];
  for (int i=0;i<n;i++) scanf("%d",&in[i]);
  for (int i=0;i<n;i++) out[in[i]]=i;
  for (int i=0;i<n;i++) printf("%d",out[i]);
  getch();
}
 
Share this answer
 
v2
C
main()
{
    int a[10],n,i,j,temp1,max,temp[10], result[10];

    scanf("%d",&n);
    for (i = 0 ; i < n ; i++)
        scanf("%d",&a[i]);       
    for (i = 0 ; i < n ; i++)
    {
        for (j = 0 ; j < n ; j++)
        {
            if(i == a[j])
            {
                result[i]=j;
                break;
            }
        }
    }

    for (i = 0 ; i < n ; i++)
        printf("%d",result[i] );
    getch();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900