好吧,那么这里是一个整数序列。在数学stackexchange,我了解了这个序列的含义。基本上:
因此,如果您有7个项目,以字母a表示,您可以将以下七个组:
1. abc
2. ade
3. afg
4. bdf
5. beg
6. cdg
7. cef'a'和'b'只在一起出现一次,'a'和'c'以及其他每一对都是如此。
我试着写一个小程序,可以给我这些三重奏的任何数字。现在,它与n个字符长的字符串一起工作。这是我所拥有的。我觉得它解释得很好。
var str = 'abcdefg';
var userPairs = [];
var found = 0
var x;
var y;
var z;
$('.bundles').append(str.length+'<br>');
for (x = 0; x < str.length; x += 1) {
for (y = 0; y < str.length; y += 1) {
for (z = 0; z < str.length; z += 1) {
var possible = str[x]+str[y]+str[z];
if (!tooSimilar(possible)) {
found += 1;
$('.bundles').append(found + ') ');
$('.bundles').append(possible+'<br>');
userPairs.push(str[x]+str[y]);
userPairs.push(str[y]+str[z]);
userPairs.push(str[x]+str[z]);
}
}
}
}
function tooSimilar(possible) {
if (possible[0] === possible[1] ||
possible[1] === possible[2] ||
possible[2] === possible[0]) {
console.log('repeated char');
return true;
} else if (userPairs.indexOf(possible[0]+possible[1]) !== -1 ||
userPairs.indexOf(possible[1]+possible[0]) !== -1 ||
userPairs.indexOf(possible[1]+possible[2]) !== -1 ||
userPairs.indexOf(possible[2]+possible[1]) !== -1 ||
userPairs.indexOf(possible[0]+possible[2]) !== -1 ||
userPairs.indexOf(possible[2]+possible[0]) !== -1){
console.log('repeated pair');
return true;
} else {
console.log('FOUND ONE');
return false;
}
}您可以在这里看到功能良好的JSFiddle。
它能工作7个字符或更少,给出你期望从序列中得到的三重奏的数量。但有超过七次崩溃了。
它输出的三位一体的列表总是符合标准,但它似乎缺少一些,我不知道在哪里。
发布于 2014-05-05 06:58:14
发布于 2014-08-03 21:04:47
下面是我刚刚编写的一个小python程序,它在几毫秒内给出了这些数字中的前10,000个:
from math import floor
for n in xrange(1,10000):
print int(floor((n/3)*floor((n-1)/2))+(n%3)*floor(n/6)),https://stackoverflow.com/questions/23464705
复制相似问题