实例35:

  设由n个人站成一圈,分别被编号1,2,3,4,……,n。第一个人从1开始报数,每报数位m的人被从圈中推测,其后的人再次从1开始报数,重复上述过程,直至所有人都从圈中退出。

实例解析:

  用链表求解本问题,先由n形成一个有n个表元组成的环,其中n个表元依此置值1~n。然后从环的第一个表元出发,连续略过m-1个表元,第m-1个表元的后继表元是第m个表元,将该表元从环中退出。接着再次从下一个表元出发,重复以上过程,直至环中表元都退出为止。

程序:

 #include <stdio.h>
#include <stdlib.h> //定义表中表元
struct ele{
int no; //表元的编号
struct ele *link; //指向环的下一个表元的指针
}; int main()
{
int n,m,i;
struct ele *h; //表头指针
struct ele *u; //用于指向表中下一个表元的指针
struct ele *p; //用于指向第m个表元的指针 printf("Please input n&m:\n");
scanf("%d%d",&n,&m); h = (struct ele *)malloc(sizeof(struct ele)); //形成首表元
u = (struct ele *)malloc(sizeof(struct ele));
h->no = ;
/*形成剩下的n-1个表元*/
for(i=; i<=n; i++)
{
u->link = (struct ele *)malloc(sizeof(struct ele));//为下一个表元分配一个地址
u = u->link; //将u指向先一个表元
u->no = i;
}
u->link = h; //末表元后继首元,形成环
puts("\nThe numbers of who will quit the cycle in turn are:");
while(n)
{
for(i=; i<m; i++) //掠过m-1个表元
u=u->link;
p = u->link;
u->link = p->link;
printf("%4d",p->no);
free(p);
n--;
}
printf("\n\n Press any key to quit...\n"); //printf("Hello world!\n");
return ;
}

最新文章

  1. async?
  2. Android开发学习之路-使用Handler和Message更新UI
  3. VS2005 “无法在证书存储区中找到清单签名证书”错误的解决方法
  4. 词频统计(WEB版)
  5. ifdown eth0 &amp;&amp; idup eth0 ifdown --exclude=l0 -a &amp;&amp; ifup --exclude=lo -a
  6. margin collapse 之父子关系的DIV
  7. 浅谈string
  8. Violin 琴弦介绍
  9. alpha-咸鱼冲刺day6
  10. Win10操作系统下Oracle VM VirtualBox6.0加载磁盘提示“发现无效设置”的解决方法(包括“不能桥接网卡”问题的解决方法)
  11. node-js:文摘
  12. CentOS 安装 Hadoop 手记
  13. Python 处理 CSV/EXCEL 表格文件
  14. Yocto学习笔记
  15. 48.UIButton上的字体居右对齐
  16. PAT 1054 The Dominant Color[简单][运行超时的问题]
  17. 序列化 ,hashlib ,configparser ,logging ,collections模块
  18. awk去重以某列重复的行
  19. js学习笔记32----new
  20. 离线状态 Postman不能开启Postman Interceptor解决

热门文章

  1. 別人寫的git的總結,寫自己這裡學習用
  2. percona mysql 5.7再centerOS 7上的安装
  3. windows下使用VNC进行远程连接
  4. tdf sample
  5. Android NDK r8 Cygwin CDT 在window下开发环境搭建 安装配置与使用 具体图文解说
  6. HDU 1298 T9【字典树增加||查询】
  7. caffe 学习(1) —— Classification: Instant Recognition with Caffe
  8. P2382 化学分子式
  9. 【洛谷P2607】[ZJOI2008]骑士
  10. 表达式过滤器 lowercase