1. 程式人生 > 其它 >freemaker 遍歷後臺Map獲取key value鍵值對,key為int型別

freemaker 遍歷後臺Map獲取key value鍵值對,key為int型別

技術標籤:C++堆疊stack

初步瞭解C++STL中的stack庫

stack(堆疊)

滿足先進後出原則(FILO)
就像是你往手槍裡裝子彈,第一顆裝的在最下面,最後一顆裝的在最上面,射出去的時候就是最後一顆裝的第一顆發,最先裝的就最後發。
庫名#include< stack >
函式定義stack< int >名稱;
empty() 堆疊為空則返回真
pop() 移除棧頂元素
push() 在棧頂增加元素
size() 返回棧中元素數目
top() 返回棧頂元素

題目(Rails)

There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track.

在這裡插入圖片描述
The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has N <= 1000 coaches numbered in increasing order 1, 2, …, N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1, a2, …, aN. Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station.

Input

The input consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of 1, 2, …, N. The last line of the block contains just 0.

The last block consists of just one line containing 0.

Output

The output contains the lines corresponding to the lines with permutations in the input. A line of the output contains Yes if it is possible to marshal the coaches in the order required on the corresponding line of the input. Otherwise it contains No. In addition, there is one empty line after the lines corresponding to one block of the input. There is no line in the output corresponding to the last ``null’’ block of the input.

思路

題目大致意思圖片表明的很清楚了。有一列火車編號為1至n,只有一箇中轉站(如圖所示),且火車只能向前開,問能不能調整至給定順序。
典型的堆疊問題。
對新手來說,每次迴圈注意清空堆疊。

程式碼

#include<iostream>
#include<stack>
using namespace std;
int main() {
    int i,n,z=0;
    stack <int>st;
    while (cin >> n)
    {
        while (!st.empty())st.pop();//堆疊清零
        int a[1010] = {0};
        if (n == 0)
        {
            return 0;
        }
        if (z) {
            cout << endl;
        }
        while (cin >> a[0])
        {
            while (!st.empty())st.pop();
            if (a[0] == 0)
            {
                break;
            }
            for (i = 1; i < n; i++)
            {
                cin >> a[i];
            }
            int k = 0;
            int t;
            for (i = 1; i <= n; i++)
            {
                st.push(i);
                if (i == a[k])
                {
                    do {
                        st.pop();
                        k++;
                        if (st.empty()) {
                            t = -1;
                        }
                        else
                            t = st.top();
                    } while (t == a[k]);
                }
            }
            if (st.size())
            {
                puts("No");
            }
            else
            {
                puts("Yes");
            }
        }
        z++;
    }
    return 0;
}