本文介绍了重复算法的排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在Fortran中编写一个代码,该代码生成了以下输入1,2,3,并用重复生成了排列:
111 112 113 121 122 123 。 。 。
显然会有3 ^ 3 = 27(n ^ k)的组合。有没有人知道生成这样的算法的算法?
解决方案以下是一个解决方案:
$ p $ module perm_mod 包含子程序print_permutations(A)隐式无整数,意图(in):: A (:) 整数:: i,j,l,余数 integer :: idx(size(A,1)),stride(size(A,1)) l =大小(A,1) 步幅(1)= 1 do i = 2,l 步幅(i)=步幅(i-1)* l enddo! i $ b do i = 0,l ** l-1 remaining = i do j = 1,1,-1 idx(j)=余数/ stride(j)余数=余数 - idx(j)* stride(j) enddo! j print *,A(idx + 1) enddo! i 结束子程序结束模块 程序权限使用perm_mod 隐含无整数,参数:: A( 3)= [1,2,3] call print_permutations(A) end program
I'm trying to write a code in Fortran that generates that given the following input 1,2,3 generates the permutations with repetition:
111 112 113 121 122 123 . . .
Obviously there will be 3^3 = 27 (n^k) combinations. Does anyone know the algorithm that generates something like this?
解决方案Here is one solution:
module perm_mod contains subroutine print_permutations(A) implicit none integer,intent(in) :: A(:) integer :: i, j, l, remainder integer :: idx(size(A,1)), stride(size(A,1)) l = size(A,1) stride(1) = 1 do i=2,l stride(i) = stride(i-1)*l enddo ! i do i=0,l**l-1 remainder = i do j=l,1,-1 idx(j) = remainder / stride(j) remainder = remainder - idx(j)*stride(j) enddo ! j print *,A(idx+1) enddo ! i end subroutine end module program perm use perm_mod implicit none integer,parameter :: A(3) = [ 1, 2, 3 ] call print_permutations(A) end program
更多推荐
重复算法的排列?
发布评论