题意:输入一组数(不知道多少个),你要把这些数字通过指定的操作变为有序的(必须为从小到大)
操作:flip(n).将第n个数字到最上面的这个序列反转,(从下往上查)
比如 5 这个序列进行flip(1)得到 1 。
4 2
3 3
2 4
1 5
输出为先输出一边原序列,然后在输出操作顺序,使序列变得符合要求;
思路:其实这个思路是跟选择排序的思想一样的,我们先将最下面的变为最大的,然后将第二大的放在最大的上面,一步步使这个序列有序;
主要是对输入的处理比较麻烦,不告诉我们要输入几个数字。。。写的时候参考了网上的一篇文章(作者为:泥瓦匠BYSocket),所以代码基本一样;
#include<bits/stdc++.h>
using namespace std;
int main()
{
for(string strline; getline(cin,strline); cout<<"0"<<endl) //很奇怪的循环结构,学到了
{
cout << strline << endl;//输出原始序列
stringstream S(strline);
deque <int> dq;
for(int num; S >> num; dq.push_front(num)) ;//将这些数字反序导入dq
for(deque<int>::iterator it = dq.begin(); it != dq.end(); it++)
{
deque<int>::iterator iMax = max_element(it,dq.end());//找到从it位置到结尾最大的元素位置
if(iMax != it)//如果不是it
{
if(iMax != dq.end() - 1)//如果最大值位置不在最上面
{
reverse(iMax,dq.end());//将最大值翻到最上面
cout << distance(dq.begin(),iMax) + 1 <<" ";//输出操作
}
reverse(it,dq.end());//将it到最上面反转
cout << distance(dq.begin(),it) + 1 << ' ';//输出操作
}
}
}
return 0;
}