[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

字符串全排列

本帖最后由 happy886rr 于 2017-4-8 09:38 编辑

字符串 def 的全排列为:def、dfe、edf、efd、fde、fed。那么字符串bathome的全排列是?
(不限编程语言,能输出正确的全排列即可,代码越少越好。)

示例代码: C
  1. #include <stdio.h>
  2. #include <string.h>
  3. void swap(char *a, char *b)
  4. {
  5.       char tmp = *a;
  6.       *a = *b;
  7.       *b = tmp;
  8. }
  9. void arrange(char *str, int start, int end)
  10. {
  11.       int i;
  12.       if(start == end)
  13.       {
  14.             printf("%s\n",str);
  15.       }else{
  16.             for(i = start; i < end; i++)
  17.             {
  18.                   swap(str+start,str+i);
  19.                   arrange(str,start+1,end);
  20.                   swap(str+start,str+i);
  21.             }
  22.       }
  23. }
  24. int main(void)
  25. {
  26.       char str[10]="bathome";
  27.       int len = strlen(str);
  28.       arrange(str,0,len);
  29.       return 0;
  30. }
复制代码
示例代码: js混编
  1. 1>1/* :
  2. @echo off
  3. cscript -nologo -e:jscript "%~f0"  %*
  4. pause&exit /b
  5. */
  6. permutations('bathome'.split(''));
  7. function permutations(arr)
  8. {  
  9. (function exfn(source, result)
  10. {  
  11. if(source.length == 0){
  12. WSH.echo(result.join(''));
  13. }else{
  14. for (var i=0; i<source.length; i++){
  15. exfn(source.slice(0, i).concat(source.slice(i+1)), result.concat(source[i]));
  16. }
  17. }
  18. })(arr, []);  
  19. }
复制代码
3

评分人数

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

TOP

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

TOP

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

TOP

回复 16# flashercs

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

TOP

返回列表