给出一个序列,然后求出转换后的序列,两种互相转换
{ 5,9,1,8,2,6,4,7,3 }
has the inversion table
2 3 6 4 0 2 2 1 0
since there are 2 numbers, 5 and 9, to the left of 1; 3 numbers, 5, 9 and 8, to the left of 2; etc.
第二种就是逆过去,拿出一个数n,一直找到一个位置,他前面刚好n个空位置,那么这个数n就在此位置
1 #include <stdio.h>
2 #include <string.h>
3 int main()
4 {
5 int n,m,i,j,a[60],b[60];
6 char ch[5];
7 while(1)
8 {
9 scanf("%d",&n);
10 if(n==0)break;
11
12
13 scanf("%s",ch);
14 for(i=0;i<n;i++)
15 scanf("%d",&a[i]);
16 if(ch[0]=='P')
17 {
18 for(i=0;i<n;i++)
19 {
20 m=0;
21 for(j=0;;j++)
22 {
23 if(a[j]>i+1)
24 m++;
25 else if(a[j]==i+1)
26 break;
27 }
28 b[i]=m;
29 }
30 }
31 else
32 {
33 bool c[60];
34 memset(c,1,sizeof(c));
35 for(i=0;i<n;i++)
36 {
37 j=0;
38 int m=0;
39 while(j!=a[i])
40 {
41 if(c[m++])j++;
42 }
43 while(!c[m])
44 m++;
45 b[m]=i+1;
46 c[m]=0;
47 }
48 }
49 for(i=0;i<n-1;i++)printf("%d ",b[i]);
50 printf("%d\n",b[i]);
51 }
52 return 0;
53 }
54