排列 [英] permutations
本文介绍了排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做一个小项目,对编程还是陌生的.
我需要做一个清单,在该清单中,我可以烫发12个中的4个部分中的12个中的任意1个,而且我不知道从哪里开始.
谁能帮忙.
亲切的问候
jim
Hi,
I am doing a small project and I am new to programming.
I need to do a list where I can perm any 1 from 12 from 4 sections of 12 and I do not have a clue where to start.
Can anyone assist please.
kind regards
jim
推荐答案
您应该使用STL容器和算法,但需要学习它...
您的马匹应该放在 std :: list< int> [^ ],然后使用一种算法功能,可能是生成 [
You should use STL containers and algorithms, but you need to learn it...
Your horses should be in an std::list< int >[^], then use one of algorithm function, may be generate[^] with your lambda function as third parameter.
听起来像是要组合而不是排列.无需使用STL容器.您可以尝试类似的方法,我用它来确认用牛顿-吉拉德(Newton-Girard)进行默认计算的第k次.
基本上,创建一个编号系统并使用严格增加的#遍历有序集.
It sounds like you want combinations and not permutations. There is no need to use an STL container. You can try something like this, which I utilized to confirm a k-th to default computation I made with Newton-Girard.
Basically, create a numbering system and iterate over the ordered sets with strictly increasing #''s.
namespace {
void print(int *s, int const & k) {
for (int j=0; j<k; j++)
printf("%i ", s[j]+1);
printf("\n");
}
int diff_sum(int *s, int const & k) {
int sum=0;
for (int j=1; j<k; j++)
sum+=s[j]-s[j-1];
return sum;
}
};
void combination_list(int const & N, int const & k) {
int selection[k], reset[k-1];
selection[0]=N-k;
int i;
for (i=1; i<k; i++)
selection[i]=reset[i-1]=N-k+i;
int count=1;
print(selection, k);
int j;
while (selection[0]+diff_sum(selection, k)>=k) {
for (j=0; j<k; j++) {
if ( diff_sum(&selection[j], k-j)==k-1-j) {
selection[j]-=1;
for (i=j+1; i<k; i++)
selection[i]=reset[i-1];
count++;
print(selection, k);
}
}
}
printf("count = %i\n", count);
}
这篇关于排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文