64,654
社区成员
发帖
与我相关
我的任务
分享
//STL就是好啊,next_permutation可以计算一组数据的全排列
// next_permutation
#i nclude <iostream>
#i nclude <algorithm>
using namespace std;
int main () {
int myints[] = {1,2,3};
cout << "The 3! possible permutations with 3 elements:\n";
sort (myints,myints+3);
do {
cout << myints[0] << " " << myints[1] << " " << myints[2] << endl;
} while ( next_permutation (myints,myints+3) );
return 0;
}
http://www.cnblogs.com/nokiaguy/archive/2008/05/11/1191914.html
#include <stdio.h>
int n = 0;
void swap(int *a, int *b)
{
int m;
m = *a;
*a = *b;
*b = m;
}
void perm(int list[], int k, int m)
{
int i;
if(k > m)
{
for(i = 0; i <= m; i++)
printf("%d ", list[i]);
printf("\n");
n++;
}
else
{
for(i = k; i <= m; i++)
{
swap(&list[k], &list[i]);
perm(list, k + 1, m);
swap(&list[k], &list[i]);
}
}
}
int main()
{
int list[] = {1, 2, 3, 4, 5};
perm(list, 0, 4);
printf("total:%d\n", n);
return 0;
}
#include <iostream>
using namespace std;
int a[100];
void comb(int m,int k)
{
int i,j;
for (i=m;i>=k;i--) {
a[k]=i;
if (k>1)
comb(i-1,k-1);
else
{
for (j=a[0];j>0;j--)
cout << a[j];
cout << endl;
}
}
}
void main()
{
a[0]=3;
comb(5,3);
}