📄 pr0629.cpp
字号:
// Programming with C++, Second Edition, by John R. Hubbard
// Copyright McGraw-Hill, 2000
// Example 6.29 on page 145
// Implementing the perfect shuffle of an array
#include <cassert> // defines the assert() function
#include <iostream> // defines the cout object
using namespace std;
const int SIZE=100;
void print(int[],int);
void shuffle(int[],int);
int main()
{ // tests the sort() function:
int a[] = { 11, 22, 33, 44, 55, 66, 77, 88 };
print(a,8);
shuffle(a,8);
print(a,8);
}
void print(int a[], int n)
{ for (int i=0; i<n-1; i++)
cout << a[i] << ", ";
cout << a[n-1] << endl;
}
void shuffle(int a[], int n)
{ // The Perfect Shuffle for an even number of elements:
assert(n <= SIZE);
int temp[SIZE];
for (int i=0; i<n/2; i++)
{ temp[2*i] = a[i];
temp[2*i+1] = a[n/2+i];
}
for (int i=0; i<n; i++)
a[i] = temp[i];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -