UVA540 - Team Queue

这个题是我放了好久才过的:开始的时候,我的思路并没有错误,不过程序的逻辑上有问题,对于,team_num.cout 在data出栈时我忘了变化。后来,我花费了一个晚上,终于找到错误所在,真的是找自己的错误比挑别人的错误难的多啊,顺着自己的思路找了好久也没找到。最后,在大神的一组数组测试下。我终于找到了wrong;由此,我在说一句我颇有感触的一句话:对于一个看着非常复杂的题或者一个程序,从心底里你不能害怕它,要勇敢的面对才是。

先插入一组测试数据:

4
4 0 1 2 3
4 4 5 6 7
4 8 9 10 11
4 12 13 14 15
ENQUEUE 6
ENQUEUE 14
ENQUEUE 1
ENQUEUE 11
ENQUEUE 2
ENQUEUE 4
ENQUEUE 13
ENQUEUE 15
ENQUEUE 12
ENQUEUE 7
ENQUEUE 9
ENQUEUE 10
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
ENQUEUE 8
ENQUEUE 12
ENQUEUE 6
ENQUEUE 3
ENQUEUE 5
ENQUEUE 1
ENQUEUE 4
ENQUEUE 15
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
DEQUEUE
STOP
0
Scenario #1
6
4
7
14
13
15
12
1
2
3
1
11
9
10
8
12
15
6
5
4


代码如下:

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
struct list
{
    int l_data;
    list *next;
};
struct teamnum
{
    list *head, *tail;
    int count;//记录该分组现有已入栈的成员数
};
int team_mate[1000050];//记录data对应的分组
teamnum team_num[1050];//记录栈中目前的组目情况
list *head = NULL;
int input (int n)
{
    int t,data;
    for(int i = 1; i <= n; i++)
    {
        cin>>t;
        for(int j = 0; j < t; j++)
        {
            cin>>data;
            team_mate[data] = i;
        }
    }
    return 0;
}
int enqueue ()
{
    int teg;
    cin>>teg;
    list *p = new list;
    p->l_data = teg;
    p->next = NULL;
    if(head == NULL)//空表情况
    {
        head = p;
        head -> next = NULL;
        team_num[team_mate[teg]].count++;
        team_num[team_mate[teg]].head = head;
        team_num[team_mate[teg]].tail = head;
    }
    else
    {
        if(team_num[team_mate[teg]].count == 0)//栈中木有自家成员
        {
            list *q;
            for(q = head; q -> next!=NULL; q = q->next);
            q -> next = p;
            team_num[team_mate[teg]].count++;
            team_num[team_mate[teg]].tail = team_num[team_mate[teg]].head = p;
        }
        else//栈中已有自家成员
        {
            list *q;
            team_num[team_mate[teg]].count++;
            q = team_num[team_mate[teg]].tail;
            p -> next = q -> next;
            q -> next = p;
            team_num[team_mate[teg]].tail = p;
        }
    }
    return 0;
}
int dequeue ()//出栈
{
    list *q = head;
    cout<< head->l_data<<endl;
    team_num[team_mate[head->l_data]].count--;//错误一:出栈时,忘了对count变化!!!
    team_num[team_mate[head->l_data]].head = head->next;
    head = head->next;
    delete q;
    return 0;
}
int main ()
{
    int n;
    int num = 0;
    while(cin>>n&&n)
    {
        memset(team_mate,0,sizeof(team_mate));
        memset(team_num,0,sizeof(team_num));
        cout<<"Scenario #"<<++num<<endl;
        head = NULL;
        input(n);
        string temp;
        while(1)
        {
            cin>>temp;
            if(temp == "ENQUEUE")
                enqueue();
            else if(temp == "DEQUEUE")
                dequeue();
            else if(temp == "STOP")
                break;
        }
        cout<<endl;
    }
    return 0;
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值