成绩 | 5 | 开启时间 | 2023年12月4日 星期一 08:00 |
折扣 | 0.8 | 折扣时间 | 2023年12月24日 星期日 23:55 |
允许迟交 | 否 | 关闭时间 | 2023年12月31日 星期日 23:55 |
幼儿园的小朋友们刚学习了如何数数,阿姨在下课时组织大家一起玩游戏。规则如下:所有的小朋友绕成一圈,顺序排号,从第一个小朋友开始报数,凡是报到固定数字(例如5)的,都退出该游戏,直到只剩下一位小朋友游戏才中止。
每个小朋友都希望自己能有更多的练习数数的机会,所以都希望成为最终被留下的那位。
现在,请大家帮小朋友们计算一下,在第一次排号的时候排到第几位才能成为最终被留下的小朋友。
输入:
小朋友的个数(此题没有规定数目,原题是 <= 50) 要被练习的数字
输出:
最终被留下的小朋友的序号
说明:
如“要被练习的数字”是5,则每次数到5的同学要退出该游戏
测试输入 | 期待的输出 | 时间限制 | 内存限制 | 额外进程 | |
---|---|---|---|---|---|
测试用例 2 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
(刚刚学链表,有什么简化的想法欢迎来讨论)
#include <stdio.h>
#include <stdlib.h>
struct linknode{
int data;
struct linknode *next;
};
struct linknode *init_linklist()
{
struct linknode *header = (struct linknode *)malloc (sizeof (struct linknode));
header -> data = -1;
header -> next = NULL;
return header;
}
void appendlinknode(struct linknode *header, int newval)
{
struct linknode *current = header;
while(current -> next != NULL)
{
current = current -> next;
}
struct linknode *newnode = (struct linknode *)malloc(sizeof(struct linknode));
newnode -> data = newval;
newnode -> next = NULL;
current -> next = newnode;
}
void converttocircular(struct linknode *header) {
struct linknode *current = header;
while (current->next != NULL) {
current = current->next;
}
current->next = header ;
}
int countcircularnode(struct linknode *header)
{
struct linknode *current = header ;
int counter=0;
while(current -> next != header )
{
counter++;
current = current -> next;
}
return counter;
}
int main()
{
int i;
int n,m;
scanf("%d %d",&n,&m);
struct linknode *header = init_linklist();
for(i=0;i<n;i++)
{
appendlinknode(header,i+1);
}
converttocircular(header);
struct linknode *current = header ;
struct linknode *previous = header;
while(countcircularnode(header) != 1)
{
for(int j=0;j<m-1;j++)
{
previous = previous -> next;
if(previous == header)
{
previous = previous -> next;
}
if(previous -> next == header)
{
previous = previous -> next;
}
}
current = previous -> next;
previous -> next = current -> next;
free(current);
current = previous -> next;
}
printf("The left child is NO %d.\n",header -> next -> data);
return 0;
}