程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> [HOJ]1456、[POJ]2259:Team queue

[HOJ]1456、[POJ]2259:Team queue

編輯:C++入門知識

想想在大神看來或許一道簡單得不行的水題足足讓我重編了3個程序,提交了n次,調了整整兩天,菜鳥本質暴露無遺呀,唉~算了是人都會有這一步的,不過神就不一樣了哈,不過在此忽略大神的存在給自己一絲安慰吧

現在想來這個題的最主要問題就是沒把題目讀明白,人家要求一次DNQUEUE輸出一次刪除的節點,結果嘞,錯了吧,再來刪除的順序都沒能很確定,是按插入順序還是隊列中的先後。。。還有好多問題根本就沒想清楚就動手,結果就杯了個大具。。。。以後一定要弄明白題目要求再動手,雖然晚動手,但絕對是事半功倍的,謹記教訓!

言歸正傳,這是題目要求:

 

Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front of the Mensa is a team queue, for example.

In a team queue each element belongs to a team. If an element enters the queue, it first searches the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue.

Your task is to write a program that simulates such a team queue.


Input

The input will contain one or more test cases. Each test case begins with the number of teams t (1<=t<=1000). Then t team descriptions follow, each one consisting of the number of elements belonging to the team and the elements themselves. Elements are integers in the range 0 - 999999. A team may consist of up to 1000 elements.

Finally, a list of commands follows. There are three different kinds of commands:

  • ENQUEUE x - enter element x into the team queue
  • DEQUEUE - process the first element and remove it from the queue
  • STOP - end of test case

    The input will be terminated by a value of 0 for t.


    Output

    For each test case, first print a line saying Scenario #k, where k is the number of the test case. Then, for each DEQUEUE command, print the element which is dequeued on a single line. Print a blank line after each test case, even after the last one.

     

    #include 
    #include 
    #include 
    #include 
    #define MAX 1000
    #define MAXE 1000000
    
    using namespace std;
    
    struct node
    {
        int num;
        node *next;
    };
    typedef struct Team
    {
        node *front;//team中的原始(頭)成員
        node *rear;//指向末尾成員
    }team[MAX];
    int elementNum[MAXE] = {0};//記錄進隊成員號所對應的team號
    int scenario = 0;//表示是第幾組測試用例,用於之後輸出
    int order[MAX];
    int orderNum = 0;
    int dnum = 1;
    int k = 0;//有幾個隊
    bool tag[MAX];
    
    void MakeNull(Team team[],int a)//對team[a]完成初始化
    {
        team[a].front = new node;
        team[a].front->next = NULL;
        team[a].front->num = 0;
        team[a].rear = team[a].front;
    }
    void ENqueue(Team team[])
    {
        int a;//記錄以讀入數為下標所對應的team值
        int e;
        node *cell = NULL;
    
        cin>>e;
        a = elementNum[e];
        if(a == 0){
            elementNum[a] = k++;//之前沒有一個隊中的則進入對尾成為一個新隊
            MakeNull(team,k);
        }
        else
        {
            cell = new node;
            cell->num = e;
            cell->next = NULL;
            team[a].rear->next = cell;
            team[a].rear = cell;
            if(!tag[a]){
                order[++orderNum] = a;
                tag[a] = true;
            }
        }
    }
    void DNqueue(Team team[])
    {
        int a;
        node *cell = NULL;
    
             a = order[dnum];
             if(a == 0){
                    cout<<0<next == NULL){
                team[a].rear = team[a].front;
    
                tag[a] = false;
                a = order[++dnum];
                if(a == 0){
                    cout<<0<next;
             team[a].front->next = cell->next;
             if(cell->next == NULL){
                tag[a] = false;
                team[a].rear = team[a].front;
                dnum++;
             }
             cout<num<>k;
        while(k > 0){
            memset(tag,false,sizeof(tag));
            memset(order,0,sizeof(order));
            memset(elementNum,0,sizeof(elementNum));
            dnum = 1;
            orderNum = 0;
            scenario++;
            cout<>j;//每個team中的隊員數
                for(int b = 0;b < j;b++){
                    cin>>e;
                    elementNum[e] = a;//以該隊員號為下表的數組
                }
             }
    
           cin>>commend;
           while(STOP != commend){//讀入命令,分別執行
                if(commend == ENQUEUE)
                    ENqueue(team);
                 else
                    DNqueue(team);
                cin>>commend;
           }
           cout<>k;
        }
        return 0;
    }
    


     

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved