示例代码: js混编复制代码
- #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;
- }
复制代码
- 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, []);
- }
感觉他是在写诗。复制代码
- setlocal&set "s=%~1 "&if "!s: =!" == "" (echo %~2)else for %%b in (%~1)do call:perm "!s:%%b =!" "%~2 %%b"
复制代码
- from itertools import permutations as pm
- for r in pm("bathome"):
- print(''.join(r))
复制代码
- function arrange(str) {
- str = str + '';
- if (str.length <= 1) {
- return [str];
- }
- var aStrRest = arrange(str.slice(1));
- var sFirst = str.charAt(0);
- var item;
- var aReturn = [];
- for (var i = 0, l = aStrRest.length; i < l; i++) {
- item = aStrRest[i];
- for (var j = 0, litem = item.length; j <= litem; j++) {
- aReturn.push(item.slice(0, j) + sFirst + item.slice(j));
- }
- }
- return aReturn;
- }
- console.log(arrange('bathome').join('\n'));
复制代码
- @echo off & setlocal enabledelayedexpansion & mode 160, 1000
- set z=1;
- set z
- for /L %%j in (2 1 6) do (
- set /a k=%%j-1
- set x=
- for /L %%i in (1 1 !k!) do (
- set t=!z:%%i=%%j!
- set x=!x!!t:;=%%i;!
- )
- set z=!z:;=%%j;!!x!
- set z
- )
- echo;complete 6
- REM 7个元素的全排列用带分隔符的单变量存储超出批处理变量存储极限, 只能另行处理
- REM 数字形式
- REM echo;!z:;=7;!
- REM for /L %%i in (1 1 6) do (
- REM set t=!z:%%i=7!
- REM echo;!t:;=%%i;!
- REM )
- set t7=!z:;=e;!
- for /L %%i in (1 1 6) do (
- set t%%i=!z:%%i=e!
- set t%%i=!t%%i:;=%%i;!
- )
- for /L %%i in (1 1 7) do (
- for %%Q in (1:b 2:a 3:t 4:h 5:o 6:m) do for /f "tokens=1-2 delims=:" %%W in ("%%Q") do set t%%i=!t%%i:%%W=%%X!
- echo;!t%%i:;= !
- )
- echo;complete 7
- pause
- exit
格式处理一下复制代码
- Permutations[{b,a,t,h,o,m,e}]
复制代码
- StringJoin /@ Permutations[Characters["bathome"]]
复制代码
- @echo off
- REM %str%为要排列的字符串,但不能含"^
- set "str=|&<>bat"
- call :arrange "%str%"
- pause
- exit /b
- :arrange
- setlocal
- set "s=%~1"
- if not defined s (
- call :escape "%~2"
- call echo,%%r%%
- goto end
- )
- set /a "n=0"
- :loop
- set /a "m=n+1"
- call set "pre=%%s:~%n%,1%%"
- call set "rest=%%s:~,%n%%%%%s:~%m%%%"
- if not defined pre goto end
- call :arrange "%rest%" "%~2%pre%"
- set /a "n+=1"
- goto loop
- :end
- endlocal
- exit /b
- :escape
- set "r=%~1"
- set "r=%r:^=^^%"
- set "r=%r:&=^&%"
- set "r=%r:|=^|%"
- set "r=%r:<=^<%"
- set "r=%r:>=^>%"
- set "r=%r:"=^"%"
- exit /b
复制代码
- @echo off
- powershell ^
- function perm($str, $t){^
- $len=$str.length;^
- if($len -le 1){$t+''+$str}else{^
- for($i=0; $i -lt $len; ++$i)^
- {^
- perm ($str.Substring(0, $i)+''+$str.Substring($i+1, $len-$i-1)) ($t+''+$str[$i]);^
- }}^
- }^
- perm 'bathome';
- pause
Perl (不借用外部模块)复制代码
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void f(char *oldstr, char *newstr, int len, int lv)
- {
- char *ostr = (char *)malloc( len * sizeof(char) );
- for (int i = 0; i < len; i++)
- {
- strcpy( ostr, oldstr );
- newstr[lv] = ostr[i];
- newstr[lv+1] = '\0';
- if (len == 1)
- printf("%s \n", newstr);
- for (int j = i; j < (len-1); j++)
- ostr[j] = ostr[j+1];
- if ( len > 0 )
- f( ostr, newstr, len-1, lv+1 );
- }
- }
- int main(int argc, char *argv[])
- {
- char oldstr[] = "bathome";
- char *newstr = (char *)malloc( (strlen(oldstr)+1) * sizeof(char) );
- f(oldstr, newstr, strlen(oldstr), 0);
- return 0;
- }
以下拷贝自《High-Order Perl》复制代码
- my @cup;
- my @nums;
- our $len;
- @nums = (split("", "bathome"));
- $len = $#nums+1;
- &func(\@cup, \@nums);
- sub func
- {
- my ($a, $b) = (shift, shift);
- my @ar;
- my @br;
- print join(",", @{$a}),"\n" if ( scalar(@{$a}) == $len );
- for my $i ( 0 .. $#{$b} )
- {
- @ar = (@{$a}, $b->[$i]);
- @br = @{$b}[0..$i-1, $i+1..$#{$b}];
- &func(\@ar, \@br);
- }
- }
《High-Order Perl》里面有更有趣的思路,午休,有空再更复制代码
- sub permute
- {
- my @items = @{ $_[0] };
- my @perms = @{ $_[1] };
- unless (@items)
- {
- print join("", @perms) ,"\n";
- }
- else
- {
- my(@newitems,@newperms,$i);
- for $i (0 .. $#items)
- {
- @newitems = @items;
- @newperms = @perms;
- unshift(@newperms, splice(@newitems, $i, 1));
- permute([@newitems], [@newperms]);
- }
- }
- }
- # sample call:
- permute([qw(b a t h o m e)], []);
来自 perldoc -q permute, 调用示例:echo a b c | permute.pl复制代码
- #!/usr/bin/perl -n
- # Fischer-Krause ordered permutation generator
- sub permute (&@) {
- my $code = shift;
- my @idx = 0..$#_;
- while ( $code->(@_[@idx]) ) {
- my $p = $#idx;
- --$p while $idx[$p-1] > $idx[$p];
- my $q = $p or return;
- push @idx, reverse splice @idx, $p;
- ++$q while $idx[$p-1] > $idx[$q];
- @idx[$p-1,$q]=@idx[$q,$p-1];
- }
- }
- permute { print "@_\n" } split;
VBS复制代码
- Z = "1;";
- for( j = 2; j <= 7; j++ ) {
- t = Z;
- Z = Z.replace(/;/g, j + ";")
- for ( i = 1; i <= j - 1; i++ ) Z = Z + t.replace(RegExp(i, "g"), j).replace(/;/g, i + ";");
- }
- for( i = 1; i <= 7; i++ ) Z = Z.replace(RegExp(i, "g"), "bathome".substr(i - 1, 1));
- console.log(Z);
复制代码
- Z = "1;"
- For j = 2 To 7
- t = Z
- Z = Replace(Z, ";", j & ";")
- For i = 1 To j - 1
- Z = Z & Replace(Replace(t, i, j), ";", i & ";")
- Next
- Next
- For i = 1 To 7
- Z = Replace(Z, i, Mid("bathome", i, 1))
- Next
- WScript.Echo Z
有了这一个规则,我们可以 通过某个中间的排列得出下一个结果:复制代码
- /* Translate by 523066680@163.com */
- #include <stdio.h>
- void splice_and_reverse( int *arr, int p, int ubound )
- {
- int t;
- for (int i = p; i <= (ubound+p)/2 ; i++ )
- {
- t = arr[i];
- arr[i] = arr[ubound - i + p];
- arr[ubound - i + p] = t;
- }
- }
- void exchange(int *arr, int a, int b)
- {
- int t;
- t = arr[a];
- arr[a] = arr[b];
- arr[b] = t;
- }
- void print_array(int *arr, int ubound)
- {
- for (int i = 0; i <= ubound; i++)
- printf("%d", arr[i]);
- printf("\n");
- }
- int main(int argc, char *argv[] )
- {
- int arr[] = {0, 1, 2, 3};
- int ubound = sizeof(arr) / sizeof(arr[0]) - 1;
- int p, q;
- while (1)
- {
- p = ubound;
- //p 递减,直到 当前元素 > 上一个元素 ,上一个元素记为 N
- while ( arr[p-1] > arr[p] ) p--;
- if ( p <= 0 ) break;
- q = p;
- //反转 从 p 至 末尾的元素
- splice_and_reverse( arr, p, ubound );
- //q 递增,直到当前元素 > N
- while ( arr[p-1] > arr[q] ) q++;
- //交换
- exchange(arr, p-1, q);
- //打印结果
- print_array(arr, ubound);
- }
- return 0;
- }
复制代码
- @echo off & setlocal enabledelayedexpansion
- set "str[0]= b a t h o m e"
- set tm1=%time%
- for %%a in (%str[0]%) do (
- set /a n+=1
- set /a odr[!n!]=n-1
- set var[!n!]=%%~a
- )
- ::FOR的嵌套递归,借鉴了http://bbs.bathome.net/viewthread.php?tid=1701&extra=&page=2中22楼CrLf的答案。
- for /l %%a in (1, 1, %n%) do if not "%%~a"=="%n%" (
- set "for=!for!for %%!var[%%~a]! in (^!str[!odr[%%~a]!]^!) do ( set "str[%%~a]=^^^!str[!odr[%%~a]!]: %%~!var[%%~a]!=^^^!" & "
- ) else (
- set "for=!for!for %%!var[%%~a]! in (^!str[!odr[%%~a]!]^!) do ( "
- )
- set "for=!for!echo;!str[0]: = %%~!"
- for /l %%a in (1, 1, %n%) do set "for=!for!) "
- %for%
- echo; 始于%tm1% ^
- 终于%time%
- pause
my @elements = qw/a b c d/; #元素 my $seats = $#elements + 1; #数量 my @order = (0) x $seats; #初始模板 to_pattern(5, $seats, \@order); print join(",", @order); sub to_pattern #转换器 { my ($num, $seats, $o_ref) = @_; my $mod; for my $div ( 1 .. $seats ) { $mod = $num % $div; $num = int($num / $div); $o_ref->[-$div] = $mod; #倒序填入 } } |
my @elements = qw/a b c d/; my $seats = $#elements + 1; my @order = (0, 2, 1, 0); apply(\@elements, \@order); sub apply { my ($e_ref, $o_ref) = @_; my @temp = @$e_ref; my @final; for my $idx ( @$o_ref ) { push @final, splice( @temp, $idx, 1 ); } print join(",", @final),"\n"; } |
use strict; my @elements = qw/a b c d e/; my $seats = $#elements + 1; my @order = (0) x $seats; for my $n ( 0 .. factorial($seats)-1 ) { my @result; to_pattern($n, $seats, \@order); apply( \@elements, \@order, \@result ); print join(",", @result), "\n"; } sub to_pattern { my ($n, $seats, $o_ref ) = @_; my $mod; for my $div ( 1 .. $seats ) { $mod = $n % $div; $n = int($n / $div); $o_ref->[-$div] = $mod; #从右边向左填入 } } sub apply { my ($e_ref, $o_ref, $result) = @_; my @temp = @$e_ref; for my $idx ( @$o_ref ) { push @$result, splice( @temp, $idx, 1 ); } } sub factorial { my $v = shift; return ($v > 1) ? $v*factorial($v-1) : 1; } |
.say for <a b c>.permutations |
import itertools for values in itertools.permutations([1,2,3]): print (values) |
[1,2,3].permutation.to_a |
复制代码
- Include masm32rt.inc
- .const
- CrLf db 0DH,0AH,0
- .data?
- Input db 20 dup (?)
- .code
- recursion Proc Uses Ebx Eax Ecx lpStr:dword
- Mov Ebx,lpStr
- .If Byte Ptr [Ebx+1] != NULL
- Mov Al,Byte Ptr [Ebx]
- Xor Ecx,Ecx
- .Repeat
- XChg Al,[Ebx+Ecx]
- Mov Byte Ptr [Ebx],Al
- Inc Ebx
- Invoke recursion,Ebx
- Dec Ebx
- XChg Al,[Ebx+Ecx]
- Mov Byte Ptr [Ebx],Al
- Inc Ecx
- .Until Byte Ptr [Ebx+Ecx] == NULL
- .Else
- Invoke StdOut,Offset Input
- Invoke StdOut,Offset CrLf
- .EndIf
- Ret
- recursion Endp
- Start:
- Invoke ArgClC,1,Offset Input
- Invoke recursion,Offset Input
- Invoke ExitProcess,NULL
- End Start
- End
欢迎光临 批处理之家 (http://bbs.bathome.net/) | Powered by Discuz! 7.2 |