简单火车车厢重排

#include <iostream>
#include <cmath>
#include <string>
using namespace std;
const int N = 105;
int st[N];

int main()
{
    int n;
    cin >> n;
    int sp[N];
    for (int i = 0; i < n; i++)
        sp[i] = 0;
    cout << "the order of train out:" << endl;
    for (int i = 0; i < n; i++)
        cin >> st[i];
    int pos =0;
    int pos2 = 0;
    for (int i = 1; i <= n; i++)
    {
        if (st[pos] != i) {
            sp[pos2++] = i;
            cout << sp[pos2 - 1] << endl;
        }
        else {
            sp[pos2] = i;
            while (pos2 >= 0 && pos < n&&sp[pos2] == st[pos])
            {
                cout << sp[pos2] << '-' << st[pos] << endl;
                pos2--; pos++;
                //cout << pos2 <<'-'<< pos << endl;
            }
            pos2++;
        }
    }
    while (pos2 >= 0 && pos < n&&sp[pos2] == st[pos])
    {
        cout << sp[pos2] << '-' << st[pos] << endl;
        pos2--; pos++;
    }
    if (pos == n) cout << "ok" << endl;
    system("pause");
    return 0;
}
/*
5
3 4 5 2 1
*/

猜你喜欢

转载自blog.csdn.net/qq_34902939/article/details/85015816
今日推荐