例题8-1的煎饼问题
#include<bits/stdc++.h>
using namespace std;
void reverse(int* ret, int m, int n)//m-left n-right
{
int tmp = 0;
while (m < n)
{
tmp = ret[m];
ret[m] = ret[n];
ret[n] = tmp;
m++;
n--;
}
}
int main()
{
int n, index=1,num;
cin >> n;//元素的个数
int* arr = (int*)malloc(sizeof(int) * n);
for (int i = 1; i <= n; i++)
cin >> arr[i];
num = n;//中间变量
for (int i = 1; i <= n; i++)//翻转次数
{
int max = 0;
//找到最大值的下标
for (int j = 1; j <= num; j++)
{
if (arr[j] > max) {
max = arr[j];
index = j;//索引
}
}
reverse(arr, 1, index);
reverse(arr, 1, num);
num--;
}
for (int i = 1; i <= n; i++)
cout << arr[i] << endl;
return 0;
}