【題解】UVA 120 Stacks of Flapjacks

【題目敘述】https://vjudge.net/problem/UVA-120

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;

int n, tmp, a[30], idx;
string s;
vector <int> v, ans;

int main() {
    while (getline(cin, s)){
        stringstream ss(s);
        v.clear();
        n = 0;
        while (ss >> s){
            tmp = 0;
            for (int i = 0; i < s.length(); i++){
                tmp *= 10;
                tmp += s[i]-'0';
            }
            v.push_back(tmp);
            a[n] = tmp;
            n++;
        }
        for (int i = 0; i < n; i++){
            cout << a[i] << " ";
        }
        cout << "\n";
        sort(a, a+n);
        ans.clear();
        for (int i = n-1; i >= 0; i--){
            for (idx = 0; idx < n; idx++){
                if (v[idx] == a[i]) break;
            }
            if (idx == i) continue;
            if (idx != 0){
                ans.push_back(n-idx);
                reverse(v.begin(), v.begin()+idx+1);
            }
            ans.push_back(n-i);
            reverse(v.begin(), v.begin()+i+1);
        }
        if (ans.size()){
            for (int i = 0; i < ans.size(); i++){
                cout << ans[i] << " " ;
            }
        }
        cout << 0 << "\n";
    }
}
分享本文 Share with friends