【发布时间】:2011-06-17 20:46:03
【问题描述】:
我正在尝试使用递归编写 0 和 1 的所有 6 位排列(即 [0 0 0 0 0 1]、[0 0 0 0 1 0]、[0 0 0 0 1 1]、 [0 0 0 1 0 0],... [1 1 1 1 1 1])。我正在遵循我从 Yahoo! Answers 获得的提示,我得到了以下提示,但它并没有一直走到最后,并且有重复的条目。
function [c] = myIEEEBaby_all_decimals(h, n)
% n: number of characters more to add
if n == 0
converter(h);
return
end
in1 = [h 1];
in2 = [h 0];
converter(in1);
converter(in2);
if length(h) < n
myIEEEBaby_all_decimals([in1], n-1)
myIEEEBaby_all_decimals([in2], n-1)
end
end
function [d] = converter(IEEE)
% convert custom IEEE representation to decimal
IEEE = [zeros(1,6-length(IEEE)), IEEE];
s = IEEE(1);
characteristic = IEEE([2,3]);
k = find(fliplr(characteristic)) - 1;
c = sum(2.^k);
fraction = IEEE([4:6]);
f = sum(2.^-(find(fliplr(fraction))));
d = (-1)^s*2^(c-1)*(1+f);
disp([num2str(IEEE),' : ', num2str(d)]);
end
输出(MATLAB)只是:
>> myIEEEBaby_all_decimals([],6)
0 0 0 0 0 1 : 0.75
0 0 0 0 0 0 : 0.5
0 0 0 0 1 1 : 0.875
0 0 0 0 1 0 : 0.625
0 0 0 1 1 1 : 0.9375
0 0 0 1 1 0 : 0.6875
0 0 1 1 1 1 : 1.875
0 0 1 1 1 0 : 1.375
0 0 1 1 0 1 : 1.625
0 0 1 1 0 0 : 1.125
0 0 0 1 0 1 : 0.8125
0 0 0 1 0 0 : 0.5625
0 0 1 0 1 1 : 1.75
0 0 1 0 1 0 : 1.25
0 0 1 0 0 1 : 1.5
0 0 1 0 0 0 : 1
0 0 0 0 0 1 : 0.75
0 0 0 0 0 0 : 0.5
0 0 0 0 1 1 : 0.875
0 0 0 0 1 0 : 0.625
0 0 0 1 1 1 : 0.9375
0 0 0 1 1 0 : 0.6875
0 0 0 1 0 1 : 0.8125
0 0 0 1 0 0 : 0.5625
0 0 0 0 0 1 : 0.75
0 0 0 0 0 0 : 0.5
0 0 0 0 1 1 : 0.875
0 0 0 0 1 0 : 0.625
0 0 0 0 0 1 : 0.75
0 0 0 0 0 0 : 0.5
【问题讨论】:
-
这不是“排列”的意思:en.wikipedia.org/wiki/Permutation
标签: matlab recursion binary permutation