完美洗牌问题是两手洗牌,假设有三只手同时洗牌呢?那么问题将变成:输入是a1,a2,……aN, b1,b2,……bN, c1,c2,……cN,要求输出是c1,b1,a1,c2,b2,a2,……cN,bN,aN,这个时候,怎么处理?
#include <iostream>
using namespace std;
//翻转字符串时间复杂度O(to - from)
void reverse(int *a, int from, int to) {
int t;
for (; from < to; ++from, --to) {
t = a[from];
a[from] = a[to];
a[to] = t;
}
}
//循环右移num位 时间复杂度O(n)
void RightRotate(int *a, int num, int n) {
reverse(a, 1, n - num);
reverse(a, n - num + 1, n);
reverse(a, 1, n);
}
//三手洗牌
void perfectShuffle(int* a, int n) {
while (n > 1) {
RightRotate(a + 1, 1, n);
RightRotate(a + 2, 1, n * 2 - 1);
a += 3;
n--;
}
}
1587

被折叠的 条评论
为什么被折叠?



