又一次因为一个小错误,POJ上Wrong Answer了无数次。。。。。

在差不多要放弃的时候,发现了这个猥琐的不能再猥琐的bug,改完了提交就AC了,简直无语。。。。

本题wo采用模拟方法:

 1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 using namespace std;
5 struct child{
6 char name[16];
7 int id;
8 //child(string, int);
9 }cd[100];
10 void init(int n){
11 char s[16];
12 for(int i=1;i<=n;i++){
13 scanf("%s",cd[i].name);
14 cd[i].id=0;
15 }
16 }
17 int main(){
18 int n,w,s; char c;
19 scanf("%d",&n);
20 init(n);
21 scanf("%d%c%d",&w,&c,&s);
22 cd[w].id=1;
23 int pt=w;
24 int kill=0;
25 while(true){
26 int step=s%(n-kill)-1;
27 if(step<=0) step=step+n-kill;
28 /*
29 这里的取模运算是考虑到s值可能非常大,如果这样的话,模拟报数过程1。。。。s将会非常
30 耗时间。
31 至于为什么这么取模,自己算一算就明白了。
32 */
33 for(int i=1;i<=step;i++){
34 int ptr=pt%n+1;
35 while(cd[ptr].id==-1){//要跳过已经被kill的元素
36 ptr=ptr%n+1;
37 }
38 cd[ptr].id=cd[pt].id+1;
39 pt=ptr;
40 }//这里的pt指向的就是我们要删除的元素
41 cd[pt].id=-1;//将id赋值为-1,相当于删除动作
42 printf("%s\n",cd[pt].name);
43 kill++;
44 if(kill==n) break; //已经清空,跳出循环
45 int ptr=pt%n+1;
46 while(cd[ptr].id==-1){
47 ptr=ptr%n+1;
48 }
49 cd[ptr].id=1;
50 pt=ptr;
51
52 }
53 //system("pause");
54 return 0;
55 }

还有可以用双向循环链表来模拟:

 1 #include<iostream>
2 #include<algorithm>
3 #include<cstdio>
4 using namespace std;
5 struct node{
6 int id;
7 node *next,*pre;
8 node();
9 node(int);
10 };
11 node *head;
12 char name[101][20];
13 node::node(int value){
14 id=value;
15 next=pre=NULL;
16 }
17 void build(int n){
18 head=new node(1);
19 head->next=head->pre=head;
20 if(n==1) return;
21 node *p=head;
22 for(int i=2;i<=n;i++){
23 node *a=new node(i);
24 p->next=a;
25 a->pre=p;
26 p=a;
27 }
28 p->next=head;
29 head->pre=p;
30 }
31 void print(){
32 node *p=head;
33 printf("%d ",p->id);
34 p=p->next;
35 while(p!=head){
36 printf("%d ",p->id);
37 p=p->next;
38 }
39 printf("\n");
40 }
41 void joseph(int s,int k){
42 node *p=head;
43 s--;
44 while(s--) p=p->next;
45 while(p->next!=p){
46 for(int i=1;i<=k-1;i++) p=p->next;
47 printf("%s\n",name[p->id]);
48 node *front=p->pre;
49 node *rear=p->next;
50 p=rear;
51 front->next=rear;
52 rear->pre=front;
53 }
54 printf("%s\n",name[p->id]);
55 }
56 int main(){
57 int n; char c;
58 scanf("%d",&n);
59 build(n);
60 //print();
61 for(int i=1;i<=n;i++){
62 scanf("%s",name[i]);
63 }
64 int s,k;
65 scanf("%d%c%d",&s,&c,&k);
66 joseph(s,k);
67 }

猴子选大王问题: 

题目描述约瑟夫问题:有n只猴子,按顺时针方向围成一圈选大王(编号从1到n),从第1号开始报数,一直数到m,数到m的猴子退出圈外,剩下的猴子再接着从1开始报数。就这样,直到圈内只剩下一只猴子时,这个猴子就是猴王,编程求输入n,m后,输出最后猴王的编号。

输入每行是用空格分开的两个整数,第一个是 n, 第二个是 m ( 0 < m,n <=300)。最后一行是:

0 0

输出对于每行输入数据(最后一行除外),输出数据也是一行,即最后猴王的编号
样例输入

6 2
12 4
8 3
0 0

样例输出

5
1

 1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 using namespace std;
5 struct child{
6 int name;
7 int id;
8 //child(string, int);
9 }cd[100];
10 void init(int n){
11 for(int i=1;i<=n;i++){
12 cd[i].name=i;
13 cd[i].id=0;
14 }
15 }
16 void solve(int n,int s){
17 init(n);
18 cd[1].id=1;
19 int pt=1;
20 int kill=0;
21 while(true){
22 int step=s%(n-kill)-1;
23 if(step<=0) step=step+n-kill;
24 /*
25 这里的取模运算是考虑到s值可能非常大,如果这样的话,模拟报数过程1。。。。s将会非常
26 耗时间。
27 至于为什么这么取模,自己算一算就明白了。
28 */
29 for(int i=1;i<=step;i++){
30 int ptr=pt%n+1;
31 while(cd[ptr].id==-1){//要跳过已经被kill的元素
32 ptr=ptr%n+1;
33 }
34 cd[ptr].id=cd[pt].id+1;
35 pt=ptr;
36 }//这里的pt指向的就是我们要删除的元素
37 cd[pt].id=-1;//将id赋值为-1,相当于删除动作
38 kill++;
39 if(kill==n){
40 printf("%d\n",cd[pt].name); break; //已经清空,跳出循环
41 }
42 int ptr=pt%n+1;
43 while(cd[ptr].id==-1){
44 ptr=ptr%n+1;
45 }
46 cd[ptr].id=1;
47 pt=ptr;
48
49 }
50 }
51 int main(){
52 int n,s;
53 while(scanf("%d%d",&n,&s)!=EOF&&n&&s){
54 solve(n,s);
55 }
56 return 0;
57 }

 

 

POJ3750: 小孩报数问题+一道经典约瑟夫问题(猴子选大王)的更多相关文章

  1. 约瑟夫环问题(猴子选大王)PHP版

    约瑟夫斯问题问题有时候也被描述成猴子选大王问题,题目如下.(最后会贴上约瑟夫问题的来历) 一群猴子排成一圈,按1,2,…,n依次编号. 然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数,再 ...

  2. (顺序表应用5.1.1)POJ 3750 小孩报数问题(基本的约瑟夫环问题:给出人数n,出发位置w,间隔数s)

    /* * POJ_3750.cpp * * Created on: 2013年10月30日 * Author: Administrator */ #include <iostream> # ...

  3. java实现猴子选大王问题(约瑟夫问题)

    题目:m只猴子围成一圈报数,报n的猴子自动离开,然后下一位重新从1开始报数,一直循环,最后剩下的那个猴子就是猴大王,写出程序求出最后是大王的那只猴子最初的位置. package learn; impo ...

  4. 算法_php猴子选大王_约瑟夫问题

    题目: n个猴子围坐一圈,从第一个猴子开始数,到第m个出列,求最后一个猴子的编号. 分析: 首先想到循环,然后队列,然后堆,所以用数组模拟一个循环的列表,下标为[0-(n-1)],下标+1整除m干掉元 ...

  5. 猴子选大王 (约瑟夫环)(c#)

    猴子选大王问题: 一堆猴子都有编号,编号是1,2,3 ...m ,这群猴子(m个)按照1到m的顺序围坐一圈, 从第1开始数,每数到第n个,该猴子就要离开此圈,这样依次下来,直到圈中只剩下最后一只猴子, ...

  6. cdoj525-猴子选大王 (约瑟夫环)

    http://acm.uestc.edu.cn/#/problem/show/525 猴子选大王 Time Limit: 3000/1000MS (Java/Others)     Memory Li ...

  7. 约瑟夫问题 小孩报数问题poj3750

    小孩报数问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15228   Accepted: 6778 Descripti ...

  8. POJ 3750 小孩报数问题 (线性表思想 约瑟夫问题 数组模拟运算的 没用循环链表,控制好下标的指向就很容易了)

    小孩报数问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10423   Accepted: 4824 Descripti ...

  9. 解析js中作用域、闭包——从一道经典的面试题开始

    如何理解js中的作用域,闭包,私有变量,this对象概念呢? 就从一道经典的面试题开始吧! 题目:创建10个<a>标签,点击时候弹出相应的序号 先思考一下,再打开看看 //先思考一下你会怎 ...

随机推荐

  1. SQL sum case when then else【转】

    数据库 t 表     b 表内容        Id        Name      胜负        1          张三     胜        2          李四     ...

  2. Linux(CentOS)常用命令

    http://fedoranews.org/alex/tutorial/rpm/3.shtml rpm.org rpm -qa|grep mysql 查询已安装的含有mysql的包. mv 移动文件. ...

  3. Bootstrap 貌似不错,先做一下记录

    Bootstrap 简洁.直观.强悍的前端开发框架,让web开发更迅速.简单. http://www.bootcss.com/

  4. [codility]Equi-leader

    http://codility.com/demo/take-sample-test/equileader 一开始想到从左和右两边开始扫取众数,但求众数又要重新扫一遍,这样复杂度就是O(n^2)了.此题 ...

  5. Linux下使用clock_gettime给程序计时

    http://www.cnblogs.com/daqiwancheng/archive/2010/07/01/1769522.html

  6. comm命令——

    comm命令 :对已经有序的文件进行比较——第一列只在文件1中出现的文件,第二列只在文件2中出现的文件,第三列在文件1和文件2中同事出现的文件 请注意前提条件:             comm对文件 ...

  7. Date简介

    Date类 在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理.这里简单介绍一下Date ...

  8. tlplayer,wzplayer所有平台通用加密测试视频

    此视频文件为通用版本,支持tlplayer,wzplayer,能在ios,android,windows,mac等平台上使用,发布此文件紧为方便用户测试. 下载地址:http://www.coolra ...

  9. Android开发之PendingIntent的使用

    PendingIntent,待确定的意图,等待的意图 官网链接:http://developer.android.com/reference/android/app/PendingIntent.htm ...

  10. Hadoop家族学习路线图

    主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, Zookeeper, Avro, Ambari, Chukwa,新增加的项 ...