65.大家一起做游戏*

成绩5开启时间2023年12月4日 星期一 08:00
折扣0.8折扣时间2023年12月24日 星期日 23:55
允许迟交关闭时间2023年12月31日 星期日 23:55

幼儿园的小朋友们刚学习了如何数数,阿姨在下课时组织大家一起玩游戏。规则如下:所有的小朋友绕成一圈,顺序排号,从第一个小朋友开始报数,凡是报到固定数字(例如5)的,都退出该游戏,直到只剩下一位小朋友游戏才中止。

每个小朋友都希望自己能有更多的练习数数的机会,所以都希望成为最终被留下的那位。

现在,请大家帮小朋友们计算一下,在第一次排号的时候排到第几位才能成为最终被留下的小朋友。

输入:
        小朋友的个数(此题没有规定数目,原题是 <= 50)   要被练习的数字

输出:
        最终被留下的小朋友的序号

说明:
        如“要被练习的数字”是5,则每次数到5的同学要退出该游戏

   

测试输入期待的输出时间限制内存限制额外进程
测试用例 2以文本方式显示
  1. 4 3↵
以文本方式显示
  1. The left child is NO 1.↵
1秒64M0

(刚刚学链表,有什么简化的想法欢迎来讨论)

#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; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值