1~m迄の数字をn個重複しないように並べる。
実行結果 m=6、n=3
1 2 3 1 2 4 1 2 5 1 2 6 1 3 4 1 3 5 1 3 6 1 4 5 1 4 6 1 5 6 2 3 4 2 3 5 2 3 6 2 4 5 2 4 6 2 5 6 3 4 5 3 4 6 3 5 6 4 5 6 Press any key to continue . . .
Fortran2003 版
module m_sub implicit none contains recursive subroutine s_cmb(m, n, list) integer, intent(in) :: m, n, list(:) integer :: i if (n == 1) then print '(25i3)', list else do i = list(size(list)) + 1, m call s_cmb(m, n - 1, [list, i]) end do end if return end subroutine s_cmb subroutine cmb(m, n) integer, intent(in) :: m, n integer :: i do i = 1, m call s_cmb(m, n, [i]) end do return end subroutine cmb end module m_sub program combi03 use m_sub implicit none call cmb(6, 3) stop end program combi03
FORTRAN77 版
PROGRAM cmbntn PARAMETER(nmax = 6, n = 3) INTEGER m(n) i = 1 m(1) = 0 1 CONTINUE IF (m(i) .LT. nmax) THEN m(i) = m(i) + 1 IF (i .EQ. n) THEN PRINT '(25i3)', m ELSE i = i + 1 m(i) = m(i - 1) END IF ELSE IF (i == 1) GOTO 999 ! exit i = i - 1 END IF GOTO 1 999 STOP END