不敢死队

http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2056&cid=1156

View Code
#include<stdio.h>  
#include<stdlib.h>  
struct node  
{  
    int num ;  
    struct node *next ;  
};  
struct node *creat(int n)  
{  
    int i ;  
    struct node *head, *p, *tail ;  
    p = (struct node*)malloc(sizeof(struct node)) ;  
    p->num = 1 ;  
    p->next = NULL ;  
    head = p ;  
    tail = p ;  
    for(i=2; i<=n; i++)  
    {  
        p = (struct node*)malloc(sizeof(struct node)) ;  
        p->num = i ;  
        tail->next = p ;  
        tail = p ;  
        tail->next = NULL ;  
    }  
    tail->next = head ;  
    return head ;  
}  
void del(struct node *head, int n)  
{  
    int count = 0, i = 0 ;  
    struct node *p, *q ;  
    q = head ;  
    while(q->next!=head)  
    q = q->next ;  
    while(count<n-1)  
    {  
        p = q->next ;  
        i++ ;  
        if(i%5==0)  
        {  
            q->next = p->next ;  
            if(p->num==1)  
            break ;  
            free(p) ;  
            count++ ;  
        }  
        else  
        q = p ;  
    }  
    if(n==5)  
    printf("1\n") ;  
    else  
    printf("%d\n", count+1) ;  
}  
int main()  
{  
    int n ;  
    struct node *head ;  
    while(scanf("%d", &n), n!=0)  
    {  
        head = creat(n) ;  
        del(head, n) ;  
    }  
    return 0 ;  
}  
  

 

posted @ 2013-04-19 21:41  yelan@yelan  阅读(226)  评论(0编辑  收藏  举报