翻煎饼代码
题目描述:麦兜爱吃煎饼........
@浅夏沫若:code
#include "iostream"
using namespace std;
int counter = 0;
using namespace std;
int counter = 0;
void turn(int array2[], int n)
{
int i = 1;
int j = n;
int temp = 0;
while (i < j)
{
temp = array2[i];
array2[i] = array2[j];
array2[j] = temp;
i++;
j--;
}
}
void tranCake(int array[], int n)
{
int key = array[n];
int index = n;
if (n == 1)
return;
for (int i = 1; i < n; i++)
{
if (array[i] > key)
{
index =i;
key =array[i];
}
}
if (index == 1)
{
turn(array, n);
counter++;
}
if (index > 1 && index < n)
{
turn(array, index);
turn(array, n);
counter += 2;
}
tranCake(array, n - 1);
}
int main()
{
int n = 0;
int a[1001] = { 0 };
cin >> n;
if (n == 0)
return 0;
for (int i = 1; i <= n; i++)
cin >> a[i];
tranCake(a, n);
cout << counter << endl;
return 0;
}
{
int i = 1;
int j = n;
int temp = 0;
while (i < j)
{
temp = array2[i];
array2[i] = array2[j];
array2[j] = temp;
i++;
j--;
}
}
void tranCake(int array[], int n)
{
int key = array[n];
int index = n;
if (n == 1)
return;
for (int i = 1; i < n; i++)
{
if (array[i] > key)
{
index =i;
key =array[i];
}
}
if (index == 1)
{
turn(array, n);
counter++;
}
if (index > 1 && index < n)
{
turn(array, index);
turn(array, n);
counter += 2;
}
tranCake(array, n - 1);
}
int main()
{
int n = 0;
int a[1001] = { 0 };
cin >> n;
if (n == 0)
return 0;
for (int i = 1; i <= n; i++)
cin >> a[i];
tranCake(a, n);
cout << counter << endl;
return 0;
}