重复算法的排列?

编程入门 行业动态 更新时间:2024-10-10 04:27:54
本文介绍了重复算法的排列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图在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

更多推荐

重复算法的排列?

本文发布于:2023-11-30 21:59:36,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   排列

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!