根据索引值重新排列数组 [英] Rearrange the array according to index value

查看:79
本文介绍了根据索引值重新排列数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不清楚如何解决。



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();
   }





输入:3



2 0 1



输出

1 2 0



但是对于更多我不支持。

喜欢输入:6

4 3 0 5 1 2

输出

2 4 5 1 0 3



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

推荐答案

这个怎么样:



What about this:

#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();
}


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();
}


这篇关于根据索引值重新排列数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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