【问题标题】:representation of permutation of integer in bits以位表示整数的排列
【发布时间】:2017-11-20 16:37:56
【问题描述】:

我试图表示 N 个给定正整数的排列。
例如。表示数字 1-16 的任意排列,每个数字使用 4 位或更少位.

这个想法是我们可以用 8*4 + 4*3 + 2*2 + 1*1 = 49 位而不是 64 位来表示 16 的排列。
我有一个示例数据集如下 [10 1 3 11 2 12 8 7 4 6 9 13 15 16 5 14]。

我尝试了以下 c 程序来实现这一点,但我不确定如何在 C 程序中使用向量来存储给定整数的位置。

我有这个问题:
如果整数范围为 1-16,那么在我需要进一步计算时将存储在位置 [0] 中的内容。

以下是我用过的一段代码:

void main()
{
    int position[16];// a vector
    int buffer[16] = {10, 1,3, 11, 2, 12, 8, 7, 4, 6, 9, 13, 15, 16, 5, 14};
    int buffer_copy[16];// copy of original buffer 

    int i, j;
    for (i=0; i<16; i++)
    {
          buffer_copy[i]=buffer[i];
    }     

    for(i=0; i<16; i++)
    {
         position[buffer[i]]= i;
         printf("\n the position of element %d in position array is: %d",
                 buffer[i], position[buffer[i]]);
    }

     int next = 8;
     for (i =1; i<16; i++)
     {
        int pos, q=0;
        pos = position[i];

       // **To check the number of positions unchecked between 0 and pos.**
        for (j=0; j<pos; j++)
        {
            if(buffer_copy[j]>=0)
            {
                q=q+1;
            }
            buffer_copy[pos] =-1;
        }
        printf("\n the value for Q is :%d", q);
     }
}

上面的代码显示了以下输出,我不确定它是否正确。

位置数组中元素10的位置为:0
位置数组中元素 1 的位置为:1
位置数组中元素 3 的位置为:2
位置数组中元素 11 的位置为:3
位置数组中元素 2 的位置为:4
位置数组中元素 12 的位置为:5
位置数组中元素 8 的位置为:6
位置数组中元素 7 的位置为:7
位置数组中元素 4 的位置为:8
位置数组中元素 6 的位置为:9
位置数组中元素 9 的位置为:10
位置数组中元素 13 的位置为:11
位置数组中元素 15 的位置为:12
位置数组中元素 16 的位置为:13
位置数组中元素 5 的位置为:14
位置数组中元素 14 的位置为:15
Q 的值为:1
Q 的值为:3
Q 的值为:1
Q 的值为:5
Q 的值为:10
Q 的值为:5
Q 的值为:4
Q 的值为:3
Q 的值为:3
Q 的值为:0
Q 的值为:1
Q 的值为:1
Q 的值为:1
Q 的值为:3
Q 的值为:1

对于完成此任务的任何帮助或建议,我将不胜感激。

【问题讨论】:

标签: c compression permutation


【解决方案1】:

Lehmer 编码似乎是您实现目标的途径。
(感谢 Ian Abbott 提供技术术语。)

此时您可以将范围的第一个数字始终编码为可能数字中的索引,占用 4 位。无需猜测所需位数,始终为 4。
然后将下一个数字编码为剩余可能数字中的索引(因为第一个不会重复),即只有 15 个中的一个;仍然是 4 位。
当只剩下 8 个数字时,编码所需的位数最终下降到 3,当只剩下 4 个可能的数字时,下降到 2,1 用于在最后两个可能的数字之间进行选择,实际上 0 位用于编码最后一个可能的数字。

那就是 8*4 + 4*3 + 2*2 + 1*1 + 1*0 = 49。

例如

1,16,3,8,11,13,7,2,4,5,6,12,15,14,9,10

 1, index  0 among 16 possibles (1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,16): 0000
16, index 14 among 15 possibles (_,2,3,4,5,6,7,8,9,A,B,C,D,E,F,16): 1110
 3, index  1 among 14 possibles (_,2,3,4,5,6,7,8,9,A,B,C,D,E,F,_) : 0001
 8, index  5 among 13 possibles (_,2,_,4,5,6,7,8,9,A,B,C,D,E,F,_) : 0101
11, index  7 among 12 possibles (_,2,_,4,5,6,7,_,9,A,B,C,D,E,F,_) : 0111
13, index  8 among 11 possibles (_,2,_,4,5,6,7,_,9,A,_,C,D,E,F,_) : 1000
 7, index  4 among 10 possibles (_,2,_,4,5,6,7,_,9,A,_,C,_,E,F,_) : 0100
 2, index  0 among  9 possibles (_,2,_,4,5,6,_,_,9,A,_,C,_,E,F,_) : 0000
 4, index  0 among  8 possibles (_,_,_,4,5,6,_,_,9,A,_,C,_,E,F,_) : 000
 5, index  0 among  7 possibles (_,_,_,_,5,6,_,_,9,A,_,C,_,E,F,_) : 000
 6, index  0 among  6 possibles (_,_,_,_,_,6,_,_,9,A,_,C,_,E,F,_) : 000
12, index  2 among  5 possibles (_,_,_,_,_,_,_,_,9,A,_,C,_,E,F,_) : 010
15, index  3 among  4 possibles (_,_,_,_,_,_,_,_,9,A,_,_,_,E,F,_) : 11
14, index  2 among  3 possibles (_,_,_,_,_,_,_,_,9,A,_,_,_,E,_,_) : 10
 9, index  0 among  2 possibles (_,_,_,_,_,_,_,_,9,A,_,_,_,_,_,_) : 0
10, only one remaning, no encoding

编码所需的位数始终为 49:4*8+3*4+2*2+1+0。
前 8 个数字始终为 4 位,
接下来的 4 个数字总是 3 位,
接下来的 2 个数字总是 2 位,
倒数第二个总是 1 位,
最后一个数字总是 0 位。

注意,我刚刚发现 Ians 评论带有指向 Lehmer 代码的有趣链接。
我想这就是我上面描述的。

我更改了代码的第一部分以正确编码。
我不明白您代码的第二部分,即显示 Q 值的部分。

#include <stdio.h>

void main(void)
{
    int position[16];// a vector
    int buffer[16] = {10, 1,3, 11, 2, 12, 8, 7, 4, 6, 9, 13, 15, 16, 5, 14};
    int buffer_copy[16];// copy of original buffer

    int i, j;
    for (i=0; i<16; i++)
    {
          buffer_copy[i]=i;
    }

    for(i=0; i<16; i++)
    {    int pos=0;
         for(j=0; j<16; j++)
         {
             if(buffer_copy[j]==0)
             { /* nothing */
             } else if (buffer_copy[j]==buffer[i])
             {
                 buffer_copy[j]=0;
                 break;
             } else
             {
                 pos++;
             }
         }
         position[i]=pos;

         printf("\n the index of '%2d' among the remaining numbers is: %d",
                 buffer[i], position[i]);
    }

}

输出:

 the index of '10' among the remaining numbers is: 9
 the index of ' 1' among the remaining numbers is: 0
 the index of ' 3' among the remaining numbers is: 1
 the index of '11' among the remaining numbers is: 7
 the index of ' 2' among the remaining numbers is: 0
 the index of '12' among the remaining numbers is: 6
 the index of ' 8' among the remaining numbers is: 4
 the index of ' 7' among the remaining numbers is: 3
 the index of ' 4' among the remaining numbers is: 0
 the index of ' 6' among the remaining numbers is: 1
 the index of ' 9' among the remaining numbers is: 1
 the index of '13' among the remaining numbers is: 1
 the index of '15' among the remaining numbers is: 2
 the index of '16' among the remaining numbers is: 2
 the index of ' 5' among the remaining numbers is: 0
 the index of '14' among the remaining numbers is: 0

【讨论】:

  • 以上代码显示多个元素位于位置数组的位置 0.。这怎么可能?
  • 它是剩余可能数字中的索引,如果已经取了1-6,那么7在剩余可能数字的索引0处。我在代码之前的示例中进行了说明。
  • 我需要以位的形式压缩给定的整数,我真的需要关于如何以位打印它们的帮助。
  • @user5424164 您能否将所需的压缩输出显示为位?你需要什么形状?可能是大小为 4,4,4,...4,4,3,3,3,3,2,2,1 的大位域?还是 49 个 1 和 0 的序列?我愿意扩展这个问题的答案。
  • 如果 15 是最后两个剩余可能数字中的第二个,则它被编码为 1,有一位,位于两个剩余数字的索引 1 上。即使它是 65300 是倒数第二个剩余的数字,它也会被编码为相同的。但是,如果数字 1 是其余 16 个数字的第一个(索引 0),则必须将其编码为 0000,4 位。请阅读 Ian Abott 提供的链接(见聊天)。理解这个想法很重要。
猜你喜欢
  • 2017-09-12
  • 1970-01-01
  • 2021-03-24
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
  • 2013-06-15
  • 1970-01-01
  • 2010-10-15
相关资源
最近更新 更多