[英]How to find a number from its 3 digit combinations?
If there is a 8 digit number 65478209 (the digits in the number can repeat) and given the combinations are always in the order of the number, then if all combinations 8c3 = 56 combinations are given then what will be the shortest solution to find out the number(s) ? 如果有8位数字65478209(数字中的数字可以重复),并且给出的组合始终按数字的顺序排列,则如果给出所有组合8c3 = 56个组合,那么找出最短的解决方案是什么号码) ?
An example scenario could be a bank login where user need to enter 3 random digits of their code like 3rd, 5th and 6th, where this is one of the 8c3 combination. 一个示例场景是银行登录,其中用户需要输入其代码的3个随机数字,例如3rd,5th和6th,这是8c3组合之一。 So if all 8c3 combinations are given then what will be the solution/logic to find the number?
因此,如果给出了所有8c3组合,那么找到数字的解决方案/逻辑将是什么? (or first number if there are more numbers as a solution)
(或第一个数字,如果有更多数字作为解决方案)
For problem solving in programming language, the input will an array of 56 3 digit combinations, and we have to find the code which is "65478209" or whatever. 为了解决编程语言中的问题,输入将包含56个3位数字的数组,我们必须找到代码“ 65478209”或任何其他内容。
How about using permutation? 如何使用排列? I write the simple code in c++ style.
我用c ++风格编写了简单的代码。 Check it.
核实。
int k = 3;
string digit = "65478209";
int digitLen = digit.length();
int* indexArr = new int[digitLen];
for(int i=0; i < digitLen; i++) indexArr[i] = i;
do
{
bool isInOrder=true;
string ret = "";
for(int i=1; i < k; i++) if(indexArr[i] < indexArr[i-1])
{
isInOrder = false;
break;
}
if(!(isInOrder)) continue;
for(int i=0; i < k; i++) ret += digit[indexArr[i]];
} while(next_permutation(indexArr, indexArr+digitLen));
delete indexArr;
here is my solution. 这是我的解决方案。
vector<string> combinations;
set<int> includedDigit;
vector<int> referCnt;
// Get reference count from all precedences
for(int i=0; i < combinations.size(); i++)
{
string e = combinations[i];
for(int j=0; j < k-1; j++)
{
includedDigit.insert(e.at(j) - '0');
for(int l=j+1; l < k; l++)
{
int curDigit = e.at(l) - '0';
referCnt.push_back(curDigit);
}
}
}
// Sorting reference counts with digit
vector<pair<int,int>> ret;
for(int i=0; i < 10; i++)
{
int digitCnt = count(referCnt.begin(), referCnt.end(), i);
if(digitCnt == 0 && includedDigit.find(i) != includedDigit.end()) ret.push_back(make_pair(1, i));
else if(digitCnt != 0) ret.push_back(make_pair(digitCnt, i));
}
sort(ret.begin(), ret.end());
// Print the result
for(auto it=ret.begin(); it != ret.end(); it++)
{
pair<int,int> val = *it;
cout << val.second;
}
It works although I think it can be shorten. 尽管我认为它可以缩短,但是它可以工作。 In addition, if original digit is not kind of permutation, it should be more complex.
另外,如果原始数字不是某种排列,它应该更复杂。
k = 3
The following recursive algorithm picks all of the k-element combinations from an ordered set: 以下递归算法从有序集合中选择所有k元素组合:
i
as first element. i
作为第一个元素。 i
with each of the combinations of k-1
elements chosen recursively from the set of elements larger than i
. i
与从大于i
的元素集中递归选择的k-1
元素的每种组合结合起来。 i
in the set. i
进行上述迭代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.