﻿ 請問一個關於數學的全排列組合的寫法(頁 1) - 網路時代 - 程式語言 - Math Pro 數學補給站

## Math Pro 數學補給站's Archiver

### 請問一個關於數學的全排列組合的寫法

[code]
clear all;clc;close all;

job_num = 6
n_1 = prod(1:job_num);   % 1~job_num的累乘積值
row = n_1;
col = job_num;
allorder_K = zeros(1:job_num-1);
cnt = 0;
period = n_1/1000;

total = 0;
for allorder_m = 1:n_1
allorder_flag=zeros(1,job_num);
allorder_P = zeros(1,job_num);
for allorder_i = 1:job_num-1
position = job_num-allorder_K(allorder_i);
allorder_j = job_num;
while(allorder_j >= position)
if(allorder_flag(allorder_j)==1)
position = position-1;
end
allorder_j =allorder_j-1;
end
allorder_P(position)=job_num+1-allorder_i;
allorder_flag(position)=1;
end
for allorder_i = 1:job_num
if (allorder_flag(allorder_i)==0)
allorder_P(allorder_i)=1;
break;
end
end
%%%%%%%%以下更新k
for allorder_i = 1:job_num-1
allorder_K(job_num-allorder_i)=mod(allorder_K(job_num-allorder_i)+1,allorder_i+1);
if (allorder_K(job_num-allorder_i)~=0)
break;
end
end
%%%下面，可以引用allorder_P進行相關處理
cnt = cnt+1
allorder_P
%--------------------------------------------------------------------------
% for m =1:row
%     sort_ind = matrix(m, :);
sort_ind = allorder_P;
%--------------------------------------------------------------------------
end

[/code] 雖然我不懂 Matlab，

### excel 版的排列

Dim CurrentRow
Sub GetString()
Dim InString As String
InString = InputBox("Enter text to permute:")
If Len(InString) < 2 Then Exit Sub
If Len(InString) >= 8 Then
MsgBox "Too many permutations!"
Exit Sub
Else
ActiveSheet.Columns(1).Clear
CurrentRow = 1
Call GetPermutation("", InString)
End If
End Sub

Sub GetPermutation(x As String, y As String)
'   The source of this algorithm is unknown
Dim i As Integer, j As Integer
j = Len(y)
If j < 2 Then
Cells(CurrentRow, 1) = x & y
CurrentRow = CurrentRow + 1
Else
For i = 1 To j
Call GetPermutation(x + Mid(y, i, 1), _
Left(y, i - 1) + Right(y, j - i))
Next
End If
End Sub

[[i] 本帖最後由 荷荷葩 於 2014-9-10 11:11 PM 編輯 [/i]]