我有四个数(如1,3,6,7)怎么用c语言编写:四个数所有排列组合
#include <stdio.h>
#include <stdlib.h>
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void Permutation(int *pStr, int begin, int end)
{
if(begin == end - 1) //只剩一个元素
{
int i;
for(i = 0; i < end; i++) //打印
printf("%d ", pStr[i]);
printf("\n");
}
else
{
int k;
for(k = begin; k < end; k++)
{
swap(pStr[k], pStr[begin]); //交换两个字符
Permutation(pStr, begin + 1, end);
swap(pStr[k],pStr[begin]); //恢复
}
}
}
int main()
{
int a[] = {1,3,6,7};
Permutation(a, 0, 4);
getchar();
return 0;
}
Good job!
有帮助就行,给个满意回答吧,嘿嘿
2012-07-19
// hope you find my code help, correct me if I am wrong
1111
1113
1133
...
1367
...
6777
7777
void genPerm(char input[], char output[], int len, int num)
{
if(num == len)
{
output[4] = '\0';
cout << output << endl;
return;
}
for(int i = 0; i < len; i++)
{
output[num] = input[i];
genPerm(input, output, len, num+1);
}
}
int main()
{
char a[] = "1367";
char b[5];
doit(a,b,4,0);
return 0;
}
用c怎么写】
This is completely C code