小鑫与小伙伴

来源:互联网 发布:淘宝春夏休闲运动鞋 编辑:程序博客网 时间:2024/04/28 06:41

小鑫与小伙伴

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

这天,小鑫决定与他的小伙伴们玩一个游戏。
游戏规则是这样的:他们将会按照随机顺序围坐成一个圈,顺时针从1开始依次编号。之后从1号开始沿顺时针报数。当数到第m个人时那个人就要退出游戏。这样进行下去一直到最后只剩下一个人。它就是胜利者。
如果告诉你小鑫朋友的数量n,小鑫的编号x,你能告诉我小鑫赢得游戏了么?

输入

输入数据为多组,到文件结束。
每一组数据有一行,三个数,n、m、x。数据小于200

输出

当小鑫赢得胜利输出“Yes”,否则输出“No”

示例输入

2 2 12 2 2

示例输出

YesNo

提示

 

来源

lin

示例程序

 
#include<stdio.h>    #include<stdlib.h>    struct node    {        int num;        struct node *next;    };    struct node *creat(int n)    {        int i;        struct node *p,*tail,*head;        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;            p->next=NULL;        }        tail->next=head;        return head;    }    int sel(struct node *head,int m,int n)    {        int num=0;        int count=0;        struct node *p,*q;        q=head;        while(q->next!=head)            q=q->next;        while(count<n-1)        {            p=q->next;            num++;            if(num%m==0)            {                q->next=p->next;            free(p);            count++;        }        else            q=p;        }    return q->num;    }    int main()    {        int n,m,k,t;        struct node *head;        while(scanf("%d %d %d",&n,&m,&k)!=EOF)        {            head=creat(n);            t=sel(head,m,n);            if(t==k)                printf("Yes\n");            else                printf("No\n");        }    }   

0 0
原创粉丝点击