返回列表 发帖

字符串全排列

本帖最后由 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;
}COPY
示例代码: 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, []);  
}COPY
3

评分人数

回复 2# CrLf
plp这个也太牛了吧,骨瘦如柴啊。
setlocal&set "s=%~1 "&if "!s: =!" == ""  (echo %~2)else for %%b in (%~1)do call:perm "!s:%%b =!" "%~2 %%b"COPY
感觉他是在写诗。

TOP

回复 5# codegay
非常短,这次代码数你的最短,最好。因为题目不限语言。你几乎胜了。

TOP

回复 10# codegay
藏龙卧虎啊,你不出点题,他们怎能显山露水。

TOP

回复 16# flashercs

你的速度确实蛮快,但是那个批处理版就很慢了。

TOP

返回列表