从"int"映射到相应的排列值? [英] Mapping from "int" to corresponding permutation value?

查看:70
本文介绍了从"int"映射到相应的排列值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早上我遇到了这个问题

I ran across this question this morning.

基本上,这个问题是关于必须为6个值创建排列的数据;每个从1到38.

Basically that question is about data which has to create permutations for 6 values; each one ranging from 1 to 38.

所以,第一个排列是

1 1 1 1 1 1      [ permutation 1 ]
1 1 1 1 1 2      [ permutation 2 ]
1 1 1 1 1 3...   [ permutation 3 ]

结束于

38 38 38 38 38 38 [ permutation 38^^6 ]

输出仅由6个嵌套循环创建,每个循环的计数从1到38;每个循环从1到38.在最内层的循环中,您将打印6个循环计数器.

The output is simply created by 6 nested loops, each counting from 1 to 38; and within the inner-most loop, you print the 6 loop counters.

现在我想知道其背后的数学原理;出于好奇:

Now I am wondering about the math behind that; and out of curiosity: what would be the "function" that

  1. 计算排列索引",给定任何排列1 2 3 4 5 6
  2. 可能更有趣:它带有一个索引",例如102382;并告诉我相应的排列输出

有人知道吗?

推荐答案

它的工作原理与更改基数(二进制,八进制或十六进制)完全相同. 第一个问题很简单: 1 * 38 ^ 6 + 2 * 38 ^ 5 + 3 * 38 ^ 4 + 4 * 38 ^ 3 + ... + 6 * 38 ^ 0 第二个相反: 102382 mod 38 ...递归地 更新 让我们假设我们想将10更改为基数2:

It works exactly like a change of base (binar, octal or hex). The first one question simply: 1*38^6 + 2*38^5 + 3*38^4 + 4*38^3 +...+6*38^0 The second one reversed: 102382 mod 38... recursively UPDATE Let us assume we want change 10 to base 2:

10/2=5 remainder(modulus)  **0** 
5/2=2 remainder           **1**
2/2=1 remainder           **0**
1/2=0 remainder           **1**

后退是 1010 将M更改为基数B,将M除以B即可,剩下的将是新基数

backwards is 1010 general gave a M to change in base B, just divide M by B , and the remainder are going to be the digit in the new base

这篇关于从"int"映射到相应的排列值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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