POJ2259-Team Queue

以前在uva上做过这个题,一模一样的,但是这次还是弄了个RE,而且这次的错误与上次完全不同;

以前的错误是逻辑错误,这次的而代码错误,只是少个else。

用了一晚上的时间,用神一样的测试数据终于揪出了这个错误。

错误见代码《

测试数据见UVA540.

#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
struct node
{
    int data;
    node *next;
};
struct zu
{
    int count;
    node *tail, *head;
};
int cla[1000050];
zu z[1050];
node* head = NULL, *tail = NULL;
void input(int t)
{
    int n, temp;
    for(int i = 1; i <= t; i++)
    {
        cin>>n;
        while(n--)
        {
            cin>>temp;
            cla[temp] = i;
        }
    }
}
void enqueue()
{
    int temp;
    cin>>temp;
    node *q = (node*)malloc(sizeof(node));
    q->data = temp;
    q->next = NULL;
    if(!head)
    {
        head = tail = q;
        z[cla[temp]].count = 1;
        z[cla[temp]].head = z[cla[temp]].tail = q;
    }
    else if(!z[cla[temp]].count)//千呼万唤始出来的错误,此处少了个else,也算的上是逻辑漏洞,以为我的愿意是正确的,-_-#!
    {
        z[cla[temp]].count = 1;
        z[cla[temp]].head = z[cla[temp]].tail = q;
        tail->next = q;
        tail = q;
    }
    else
    {
        z[cla[temp]].count++;
        q->next = z[cla[temp]].tail->next;
        z[cla[temp]].tail->next = q;
        z[cla[temp]].tail = q;
        if(tail->next == q)tail = q;
    }
}
void dequeue()
{
    node *q = head;
    q = head;
    z[cla[q->data]].count--;
    z[cla[q->data]].head = z[cla[q->data]].head->next;
    cout<<q->data<<endl;
    head = head->next;
    delete q;
}
int main ()
{
    int t, num = 0;
    string s;
    while(cin>>t&&t)
    {
        head = tail = NULL;
        memset(z,0,sizeof(z));
        input(t);
        cout<<"Scenario #"<<++num<<endl;
        while(cin>>s)
        {
            cin.ignore();
            if(s == "ENQUEUE")enqueue();
            if(s == "DEQUEUE")dequeue();
            if(s == "STOP")
                break;
        }
            cout<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值