UvaLive3055 POJ2013 ZOJ2172 Symmetric Order

題目連結 UVaLive
題目連結 POJ

  • 題意:給定 $N$ 個字串,要依規定輸出:先輸出奇數項字串、再反著輸出偶數項。
    題目連結 ZOJ
  • 題解:遇到奇數項直接輸出,否則用 stack,最後再從 stack 依序 pop 輸出。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    #pragma GCC optimize(2)
    #include <iostream>
    #include <vector>
    #include <string>
    #include <stack>
    using namespace std;
    const int INF = 1e9;
    const int MXN = 0;
    const int MXV = 0;
    #define MP make_pair
    #define PB push_back
    #define F first
    #define S second
    #define FOR(i, L, R) for (int i = L; i != (int)R; ++i)
    #define FORD(i, L, R) for (int i = L; i != (int)R; --i)
    #define IOS \
    cin.tie(NULL); \
    cout.tie(NULL); \
    ios_base::sync_with_stdio(false);

    int main()
    {
    IOS;
    int n;
    int ti = 0;
    while(cin >> n, n)
    {
    stack<string> st;
    string s;
    cout << "SET " << ++ti << '\n';
    FOR(i, 0, n)
    {
    cin >> s;
    if(i % 2)
    {
    st.push(s);
    }
    else
    {
    cout << s << '\n';
    }
    }
    while(!st.empty())
    {
    cout << st.top() << '\n';
    st.pop();
    }
    }
    }

如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Recommended Posts