本帖最后由 happy886rr 于 2017-4-8 09:38 编辑
字符串 def 的全排列为:def、dfe、edf、efd、fde、fed。那么字符串bathome的全排列是?
(不限编程语言,能输出正确的全排列即可,代码越少越好。)
示例代码: C- #include <stdio.h>
- #include <string.h>
-
- void swap(char *a, char *b)
- {
- char tmp = *a;
- *a = *b;
- *b = tmp;
- }
-
- void arrange(char *str, int start, int end)
- {
- int i;
- if(start == end)
- {
- printf("%s\n",str);
-
- }else{
- for(i = start; i < end; i++)
- {
- swap(str+start,str+i);
- arrange(str,start+1,end);
- swap(str+start,str+i);
- }
-
- }
- }
-
- int main(void)
- {
- char str[10]="bathome";
- int len = strlen(str);
- arrange(str,0,len);
- return 0;
- }
复制代码 示例代码: js混编- 1>1/* :
- @echo off
- cscript -nologo -e:jscript "%~f0" %*
- pause&exit /b
- */
-
- permutations('bathome'.split(''));
-
- function permutations(arr)
- {
- (function exfn(source, result)
- {
- if(source.length == 0){
- WSH.echo(result.join(''));
- }else{
- for (var i=0; i<source.length; i++){
- exfn(source.slice(0, i).concat(source.slice(i+1)), result.concat(source[i]));
- }
- }
- })(arr, []);
- }
复制代码
|