UVa 11925 - Generating Permutations

来源:互联网 发布:时时彩组选计划软件 编辑:程序博客网 时间:2024/05/21 11:04

这题用了冒泡排序的思想,不过紫书上的描述有误,建议参考原题。

#include <bits/stdc++.h>using namespace std;deque<int> a;vector<int> res;bool is_order(){for (int i = 1; i != a.size(); ++i)if (a[i] < a[i-1])return false;return true;}int main(){int n;while (a.clear(), res.clear(), cin >> n, n){for (int i = 0; i < n; ++i){int t; cin >> t; a.push_back(t);}while (!is_order()){if (a[0]>a[1] && a[0]!=n){swap(a[0], a[1]);res.push_back(1);}else{int t = a.back();a.pop_back();a.push_front(t);res.push_back(2);}}for(vector<int>::reverse_iterator it = res.rbegin(); it != res.rend(); ++it)            cout << *it;        cout << endl;}return 0;}


0 0
原创粉丝点击