在0和1的序列中,如何保证没有相同的值连续出现超过2次?
In a sequence of 0 and 1, how to make sure that no same values appear more than 2 times consecutively?
我正在生成一个由相同数量的 0 和 1 组成的序列。 A = [ 0 1 1 0 1 1 1 0 0 1 0 0] 我的目标是不让 0 或 1 连续重复超过 2 次。在上面的数组 A 中,您可以看到 1 1 1 出现,所以我需要以某种方式将其切换,以便其中一个变为 0,如果有类似 0 0 0 的内容,则将其中一个切换为 1。我尝试了一些算法适用于几次迭代,但随后我得到不同数量的 0 和 1。此外,结果或输出应保持相同数量的 1 和 0,即六个 0 和六个 1。
nums = mod( reshape(randperm(1*12), 1, 12), 2)
for i = 1:length(nums)-2
if nums(i+1)==nums(i) & nums(i+2) ==nums(i)
if nums(i) == 1
nums(i+2) = 0;
else
nums(i+2) = 1;
end
end
end
你只需要做一个计数器。
nums = mod( reshape(randperm(1*12), 1, 12), 2)
idx=1;
counter_one=0;
counter_zero=0;
changedIndex=0;
while idx <= length(nums)
if (nums(idx) == 0)
counter_zero = counter_zero+1;
counter_one=0;
if (counter_zero == 3)
nums(idx)=1;
counter_zero=0;
changedIndex = 1;
end
else
counter_one= counter_one+1;
counter_zero=0;
if (counter_one== 3)
nums(idx)=0;
counter_one=0;
changedIndex =1;
end
end
if (changedIndex == 1)
if (idx < length(nums))
if (nums(idx+1)) == 1
counter_one = 1;
changedIndex = 0;
else
counter_zero=1;
changedIndex = 0;
end
else
if (nums(idx)) == 1
counter_one = 1;
changedIndex = 0;
else
counter_zero=1;
changedIndex = 0;
end
end
end
idx = idx+1;
end
刚刚测试了一下,效果很好
新测试,向量更难
由于您的约束过于复杂,无法解决问题后验,我建议您反复尝试,直到找到解决方案。 (考虑到输出向量的长度较小,这种方法是可行的)。
代码如下:
out=0;
while ~out
nums = mod( reshape(randperm(1*12), 1, 12), 2);
diff1=diff(nums);
diff1(diff1~=0)=NaN;
diff2=diff(diff1);
if all(diff2~=0)
out=1;
end
end
它基本上迭代了 2 个步骤:
1/ 生成候选人:
nums = mod( reshape(randperm(1*12), 1, 12), 2);
2/ 检查候选人是否遵守约束条件
diff1=diff(nums);
diff1(diff1~=0)=NaN;
这里使用 MATLAB 的 diff
函数来检查重复数字。 0 表示数字重复 2 次。为了检查一个数字是否重复了 3 次,我们需要再次使用这个函数。
diff2=diff(diff1);
现在,如果一个数字连续重复 3 次或更多次,就会出现 0,因此我们检查 diff2
中是否存在 0。如果没有任何候选人,我们将结束 while 循环。
if all(diff2~=0)
out=1;
end
示例运行:
这可能是一种不需要尝试排列直到找到排列的方法:
A = [];
N = 100; %//number of elements you desire in A (must be even).
将 A 初始化为具有相等数量的交替 0s
和 1s
:
%//initialize A = [1 0 1 0 1 0....] with equal numbers of 0 and 1.
for x=1:N/2
A = [A 1 0];
end
遍历 A
:
for x=3:(N-1) %//iterate through A, and decide to swap or not at each iteration
if ( A(x) == A(x-1) && A(x) == A(x-2) ) %//must swap
temp = A(x);
A(x) = A(x+1);
A(x+1) = temp;
elseif ( A(x+1) == A(x-1) && A(x+1) == A(x-2) ) %//must not swap
else %//can choose to swap or not
if (randi([0 1])) %should I swap?
temp = A(x);
A(x) = A(x+1);
A(x+1) = temp;
end
end
end
我正在生成一个由相同数量的 0 和 1 组成的序列。 A = [ 0 1 1 0 1 1 1 0 0 1 0 0] 我的目标是不让 0 或 1 连续重复超过 2 次。在上面的数组 A 中,您可以看到 1 1 1 出现,所以我需要以某种方式将其切换,以便其中一个变为 0,如果有类似 0 0 0 的内容,则将其中一个切换为 1。我尝试了一些算法适用于几次迭代,但随后我得到不同数量的 0 和 1。此外,结果或输出应保持相同数量的 1 和 0,即六个 0 和六个 1。
nums = mod( reshape(randperm(1*12), 1, 12), 2)
for i = 1:length(nums)-2
if nums(i+1)==nums(i) & nums(i+2) ==nums(i)
if nums(i) == 1
nums(i+2) = 0;
else
nums(i+2) = 1;
end
end
end
你只需要做一个计数器。
nums = mod( reshape(randperm(1*12), 1, 12), 2)
idx=1;
counter_one=0;
counter_zero=0;
changedIndex=0;
while idx <= length(nums)
if (nums(idx) == 0)
counter_zero = counter_zero+1;
counter_one=0;
if (counter_zero == 3)
nums(idx)=1;
counter_zero=0;
changedIndex = 1;
end
else
counter_one= counter_one+1;
counter_zero=0;
if (counter_one== 3)
nums(idx)=0;
counter_one=0;
changedIndex =1;
end
end
if (changedIndex == 1)
if (idx < length(nums))
if (nums(idx+1)) == 1
counter_one = 1;
changedIndex = 0;
else
counter_zero=1;
changedIndex = 0;
end
else
if (nums(idx)) == 1
counter_one = 1;
changedIndex = 0;
else
counter_zero=1;
changedIndex = 0;
end
end
end
idx = idx+1;
end
刚刚测试了一下,效果很好
新测试,向量更难
由于您的约束过于复杂,无法解决问题后验,我建议您反复尝试,直到找到解决方案。 (考虑到输出向量的长度较小,这种方法是可行的)。
代码如下:
out=0;
while ~out
nums = mod( reshape(randperm(1*12), 1, 12), 2);
diff1=diff(nums);
diff1(diff1~=0)=NaN;
diff2=diff(diff1);
if all(diff2~=0)
out=1;
end
end
它基本上迭代了 2 个步骤:
1/ 生成候选人:
nums = mod( reshape(randperm(1*12), 1, 12), 2);
2/ 检查候选人是否遵守约束条件
diff1=diff(nums);
diff1(diff1~=0)=NaN;
这里使用 MATLAB 的 diff
函数来检查重复数字。 0 表示数字重复 2 次。为了检查一个数字是否重复了 3 次,我们需要再次使用这个函数。
diff2=diff(diff1);
现在,如果一个数字连续重复 3 次或更多次,就会出现 0,因此我们检查 diff2
中是否存在 0。如果没有任何候选人,我们将结束 while 循环。
if all(diff2~=0)
out=1;
end
示例运行:
这可能是一种不需要尝试排列直到找到排列的方法:
A = [];
N = 100; %//number of elements you desire in A (must be even).
将 A 初始化为具有相等数量的交替 0s
和 1s
:
%//initialize A = [1 0 1 0 1 0....] with equal numbers of 0 and 1.
for x=1:N/2
A = [A 1 0];
end
遍历 A
:
for x=3:(N-1) %//iterate through A, and decide to swap or not at each iteration
if ( A(x) == A(x-1) && A(x) == A(x-2) ) %//must swap
temp = A(x);
A(x) = A(x+1);
A(x+1) = temp;
elseif ( A(x+1) == A(x-1) && A(x+1) == A(x-2) ) %//must not swap
else %//can choose to swap or not
if (randi([0 1])) %should I swap?
temp = A(x);
A(x) = A(x+1);
A(x+1) = temp;
end
end
end