• HDU 5071 模拟


    考察英语的题 - -#

    按条件模拟,一遍即可了,每一个聊天对象有其价值U。数组模拟队列过程即可,若存在Top标记,则和Top标记的人聊天,否则和队列的第一个人聊天

    mark记录队尾,top记录Top操作,data[i].p记录U,data[i].x记录chat数。data[i].y记录该人是否被删除

    Add U:在 队尾插入价值为U的人,须要特判U人已经存在

    Close U::在整个队列中查找价值为U的人,将其删除。须要特判该人不存在

    Chat x:当前聊天页面的人chat+=x,特判当前队列没有人

    Rotata x: 使当前队列排在第x位的人放在第1位,其余的依次向后退一位。特判没有第x位

    Prior: 找出当前队列中价值最大的人,把其放在第1位,其余的依次向后退一位。特判队列空

    Choose U:找到价值为U的人。把其放在第1位,其余的依次向后退一位,特判没有该价值的人

    Top U:标记价值为U的人,将其标为top,始终与她聊天,但不放到队列第一位,直到Untop。特判没有这个人

    Untop:取消TOP标记,并回复与队列第一位聊天,特判当前没有Top标记

    最后全部指令运行完后

    须要从队列第一位依次和还没有Close而且Chat!

    =0的人  运行Bye操作,若有Top,先和Top-Bye

    Bye:输出当前人的U和chat数

    #include "stdio.h"
    #include "string.h"
    
    struct node
    {
        int p,y;
        __int64 x;
    }data[50010];
    
    int mark,top;
    void Add()
    {
        int ok,x,i;
        ok=1;
        scanf("%d",&x);
    
        for (i=1;i<=mark;i++)
        if (data[i].y==0 && data[i].p==x) { ok=0; break;}
    
        if (ok==0)
        {
            printf("same priority.
    ");
            return ;
        }
        printf("success.
    ");
    
        mark++;
        data[mark].p=x;
        data[mark].x=0;
    }
    
    void Close()
    {
        int ok,x,i;
        ok=0;
        scanf("%d",&x);
        for (i=1;i<=mark;i++)
        if (data[i].y==0 && data[i].p==x)  { ok=i; break;}
    
        if (ok==0)
        {
            printf("invalid priority.
    ");
            return ;
        }
    
        data[i].y=1;
        if (top==ok) top=0;
        printf("close %d with %I64d.
    ",data[ok].p,data[ok].x);
    }
    
    void Chat()
    {
        int ok,i;
        __int64 x;
        scanf("%I64d",&x);
        if (top!=0)
        {
            data[top].x+=x;
            printf("success.
    ");
            return ;
        }
    
        ok=0;
        for (i=1;i<=mark;i++)
        if (data[i].y==0) {ok=i; break;}
        if (ok==0)
            printf("empty.
    ");
        else printf("success.
    ");
    
        data[ok].x+=x;
    
    }
    
    void Rotate()
    {
        node temp;
        int cnt,j,x,i,first;
        cnt=0;
        scanf("%d",&x);
        first=0;
        for (i=1;i<=mark;i++)
        if (data[i].y==0)
        {
            if (first==0) first=i;
            cnt++;
            if (cnt==x) break;
        }
    
        if (cnt==x)
        {
            printf("success.
    ");
            temp=data[i];
            if (top==i) top=1;
            else
                if (top!=0 && top<i) top++;
            for (j=i;j>1;j--)
                data[j]=data[j-1];
            data[1]=temp;
            return ;
        }
    
        printf("out of range.
    ");
    }
    
    void Prior()
    {
        int Max,i,id;
        node temp;
        Max=0;
        if (mark==0)
        {
            printf("empty.
    ");
            return ;
        }
        for (i=1;i<=mark;i++)
            if(data[i].y==0 && data[i].p>Max)
            {
                Max=data[i].p;
                id=i;
            }
        if (Max==0)
        {
            printf("empty.
    ");
            return ;
        }
    
        printf("success.
    ");
        temp=data[id];
        if (top==id) top=1;
        else
        if (top!=0 && top<id ) top++;
    
        for (i=id;i>1;i--)
            data[i]=data[i-1];
        data[1]=temp;
    }
    
    
    void Choose()
    {
        int ok,x,i,first;
        node temp;
        scanf("%d",&x);
        ok=0;
        first=0;
        for (i=1;i<=mark;i++)
            if(data[i].y==0)
            {
                if (first==0) first=i;
                if (data[i].p==x)
                {
                ok=i;
                break;
                }
            }
        if (ok==0)
        {
            printf("invalid priority.
    ");
            return ;
        }
    
        temp=data[ok];
        if (top==ok) top=1;
        else
        if (top!=0 && top<ok ) top++;
    
        for (i=ok;i>1;i--)
            data[i]=data[i-1];
        data[1]=temp;
        printf("success.
    ");
    }
    
    void Top()
    {
        int i,ok,x;
        scanf("%d",&x);
        ok=0;
        for (i=1;i<=mark;i++)
        if (data[i].y==0 && data[i].p==x)
        {
            top=i;
            ok=1;
            break;
        }
    
        if (ok==1) printf("success.
    ");
        else printf("invalid priority.
    ");
    }
    
    void Untop()
    {
        if (top==0) printf("no such person.
    ");
        else printf("success.
    ");
        top=0;
    }
    
    int main()
    {
        int Case,ii,i,n;
        char str[101];
        scanf("%d",&Case);
        while(Case--)
        {
            scanf("%d",&n);
            mark=0; top=0;
            memset(data,0,sizeof(data));
            for (ii=1;ii<=n;ii++)
            {
                scanf("%s",str);
                printf("Operation #%d: ",ii);
                if (strcmp(str,"Add")==0) Add();
                if (strcmp(str,"Close")==0) Close();
                if (strcmp(str,"Chat")==0) Chat();
                if (strcmp(str,"Rotate")==0) Rotate();
                if (strcmp(str,"Prior")==0) Prior();
                if (strcmp(str,"Choose")==0) Choose();
                if (strcmp(str,"Top")==0) Top();
                if (strcmp(str,"Untop")==0)  Untop();
            }
            if (top!=0 && data[top].x!=0)
            {
                printf("Bye %d: %I64d
    ",data[top].p,data[top].x);
                data[top].y=1;
            }
            for (i=1;i<=mark;i++)
                if (data[i].y==0 && data[i].x!=0)
                    printf("Bye %d: %I64d
    ",data[i].p,data[i].x);
        }
        return 0;
    }
    



  • 相关阅读:
    Linux命令(25)userdel命令
    Linux命令(24)clear命令
    Linux命令(23)usermod命令
    Linux命令(22)useradd命令
    c++primer 练习9.28
    概率论python代码
    python自写软件(三)
    Linux描述符表和描述符高速缓存
    操作系统的坑(更新)
    python自写软件(二)
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5133985.html
Copyright © 2020-2023  润新知