比较简单的一道题目,由大到小找出需要调整的数字的位置,然后将该数字翻转到最上方,然后再从目标位置进行一次翻转,将这数字调整到正确的位置,同时注意,如果该数字已经是在最上方了,那么就直接在目标位置处进行翻转就行了,具体实现见如下代码:
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
#include<functional>
using namespace std;
string s;
int amount,data[35];
void flip(int ind){
for (int i = 0; i < ind - i; i++){
swap(data[i], data[ind - i]);
}
cout <<amount-ind << " ";
}
int main(){
while (getline(cin, s)){
if (s == "0") break;
vector<int> res;
stringstream is(s);
int t;
amount = 0;
while (is >> t){
data[amount] = t;
amount++;
}
cout << s << endl;
for (int i = amount - 1; i >= 0; i--){
int ind = max_element(data, data + i + 1) - data;
if (ind == i) continue;
if (ind > 0) flip(ind);
flip(i);
}
cout << "0\n";
}
return 0;
}