UV 133-The Dole Queue // simulation, linked list

Source: Internet
Author: User

In fact, this question is not difficult, that is, the output requirements are a little higher. As a result, I have learned a lot, resulting in a long and messy code, resulting in wa and pe.

Question Analysis: If you want to open an array and simulate a ring, you can directly use an array to simulate a circular linked list + Delete. OK. An error occurred while understanding the output. As a result, WAF performed 3 times and peat twice. Here is a small trap:

If the point to be deleted is exactly the point where another person starts counting again, move one digit forward or backward.

The following code is used:

#include<stdio.h>#define MAXN 22int queue[MAXN];int prio[MAXN],next[MAXN];int n,k,m,N;void initi(){    for(int i=1;i<=n;i++)    {        queue[n-i+1]=i;        if(i==1) prio[n-i+1]=1;        else prio[n-i+1]=n-i+2;        if(i==n) next[n-i+1]=n;        else next[n-i+1]=n-i;    }}int couter_clock(int begin){    for(int i=1;i<k;i++) begin=prio[begin];    return begin;}int clock(int begin){    for(int i=1;i<m;i++) begin=next[begin];    return begin;}void del(int begin){    prio[next[begin]]=prio[begin];    next[prio[begin]]=next[begin];    N--;}int main(){    while(scanf("%d%d%d",&n,&k,&m),n,k,m){    initi();    int begin1=1;    int begin2=n;    N=n;    while(true)    {        int ans1=couter_clock(begin1);        int ans2=clock(begin2);        begin1=prio[ans1];        begin2=next[ans2];        del(ans1);        if(ans1!=ans2) del(ans2);        if(begin1==ans2) begin1=prio[begin1];        if(begin2==ans1) begin2=next[begin2];        printf("%3d",ans1);        if(ans1!=ans2) printf("%3d",ans2);        if(N>0) printf(",");        else {printf("\n");break;}    }    }    return 0;}

Hey, although the question is simple, the coding capability is improved! Keep moving!

Zys love sky and shadow!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.