1487 圆桌问题
来源:互联网 发布:360路由器端口有多少个 编辑:程序博客网 时间:2024/04/30 00:53
描述
圆桌问题
编号为1—n的n个人围坐在圆桌前,从第一个人开始报数,以1开始,报数为常数m的人离开桌子。n<=10000, m<100
下个人接着从1开始报数,依次重复进行下去,直到全部人都离开桌子。
要求输出各位离开的编号次序。
输入一共有t+1行。第一行为测试数据的组数t,下面的t行分别对应t组测试数据。每行为人数n和报数值m,两个数用空格隔开
输出输出一共有m行,分别对应m组测试数据。输出离开座位的编号次序,编号之间用空格隔开。
解题思路:此题是约瑟夫问题的变种,通过链表来做比较方便。注意判断一下边界处理一下循环即可。
#include<stdio.h>#include<stdlib.h>struct node{ int number;struct node *next;};main(){ int k=1,m,n,i,j,flag = 1,t,up; int number1; scanf("%d",&number1); for (up=1;up<=number1;up++) { struct node *head,*p,*q;head = ( struct node* )malloc( sizeof( struct node ) );head -> number = -2;head -> next = head;scanf( "%d%d",&n,&m );if( flag == 1 ){ for( i = n ; i > 0 ; i-- ){ p = ( struct node* )malloc( sizeof( struct node ));p -> next = head -> next;p -> number = i;head -> next = p;}while ( p -> next!=head )p = p -> next;p -> next = head -> next;/*建立循环链表*/for( i = 0;i < k; i++ )p = p -> next;/*找出起始位置*/ for(i = 0; i < n ; i++) { for( j = 1; j < m; j++ )p = p -> next;q = p -> next;p -> next = q -> next;t = (q -> number) -1;if(t == 0) t= t + n;if(i < n - 1) printf("%d ",t);if( i == n -1) printf("%d\n",t);free(q);/*报数找出剔除的人*/} }} }
- 1487 圆桌问题
- 圆桌问题
- 圆桌问题
- 圆桌问题
- 圆桌问题
- 圆桌问题
- 圆桌问题
- Josephus问题(圆桌骑士)
- 圆桌吃饭问题
- hdu4841 圆桌问题
- hdu4841.圆桌问题
- hdoj圆桌问题
- HDU 4841 圆桌问题
- HDOJ 圆桌问题 4841
- hdu 4841 圆桌问题
- 搬圆桌问题
- swust1740: 圆桌问题
- NKOI 1940 圆桌问题
- 1483 求原排列
- 设计模式学习之抽象工厂模式(实现数据访问层)
- 1484 分解N阶乘
- 1485 找a+b=c
- 设置上传文件大小
- 1487 圆桌问题
- 1488 偷懒的明明
- 1489 N的阶乘
- 解决pppoeconf与network-manager冲突问题
- nutch论坛
- tomcat负载均衡: Unable to start cluster receiver
- HDU 1264 Counting Squares
- add source.list in ubuntu
- 我的第一个MFC程序