|
谢谢现在应该能够完全说明本题目了,以下是楼上朋友从白度上给出的C程序,这只是本题目的基础,
全排列和全组合
#include <iostream>
#include <fstream>
using namespace std;
const count = 5;
fstream f;
void perm(char *a, const int k, const int n)
{ int i; if (k==n-1)
{ for (i=0; i<n; i++) {
cout<<a<<" ";
f<<a;
}
cout<<endl;
f<<endl;
}
else
{
for (i=k; i<n; i++)
{
char temp = a[k];
a[k] = a;
a = temp;
perm(a, k+1, n);
temp = a[k];
a[k] = a;
a = temp;
}
}
}
void comb(char* a, int* b, const int k, const int n)
{
int i;
if (k==n)
{
char temp[count];
int j=0;
for (i=0; i<n; i++)
if(b == 1)
temp[j++] = a;
if (j)
perm(temp, 0, j);
}
else
{
b[k] = 0;
comb(a, b, k+1, n);
b[k] = 1;
comb(a, b, k+1, n);
}
}
void main()
{
f.open("test.txt", ios: ut);
char ex[5]={'a', 'b', 'c', 'd', 'e'};
int a[5];
comb(ex, a, 0, 5);
f.close();
}
他的递归过程就是
void comb(char* a, int* b, const int k, const int n)
{ int i; if (k==n) {
char temp[count];
int j=0;
for (i=0; i<n; i++)
if(b == 1)
temp[j++] = a;
if (j)
perm(temp, 0, j);
} else
{ b[k] = 0;
comb(a, b, k+1, n);
b[k] = 1;
comb(a, b, k+1, n);
}
}
该过程显然有很多分号,题目的要求就是“没有分号” |
|