Description

The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, . . ., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.

Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.

Input

The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14.

Output

The output file will consist of separate lines containing m corresponding to k in the input file.

Sample Input

3
4
0

Sample Output

5
30
题目大意:
有k个好人和k个坏人,从第一个人开始循环报数(即报到m后从1开始报),报到m的人被处死,要求全部的坏人都处死,好人留下,求出m。
这个代码就是有点慢,所以我打了个表

#include<iostream>
using namespace std;
int main()
{
int k;
while(cin>>k)
{
if(!k)
break; int m=1;
int ans[30]={0};
for(int i=1;i<=k;i++)
{
ans[i]=(ans[i-1]+m-1)%(2*k-i+1);
if(ans[i]<k)
{
i=0;m++;
}
}
cout<<m<<endl; } return 0;
} }
sort(yes,yes+n,cmp); sort(no,no+n,cmp);
cout<<yes[n-1]<<" "<<no[n-1]<<endl; return 0; }

  

#include<stdio.h>
int main()
{
int k;
int Joseph[14]={0,2,7,5,30,169,441,1872,7632,1740,93313,459901,1358657,2504881};
while(scanf("%d",&k),k)
printf("%d\n",Joseph[k]);
return 0;
}

  

poj-1012-约瑟夫问题的更多相关文章

  1. Joseph POJ - 1012 约瑟夫环递推

    题意:约瑟夫环  初始前k个人后k个人  问m等于多少的时候 后k个先出去 题解:因为前k个位置是不动的,所以只要考虑每次递推后的位置在不在前面k个就行 有递推式 ans[i]=(ans[i-1]+m ...

  2. (顺序表的应用5.4.3)POJ 1012(约瑟夫环问题——保证前k个出队元素为后k个元素)

    /* * POJ-1012.cpp * * Created on: 2013年10月31日 * Author: Administrator */ #include <iostream> # ...

  3. poj 1012 &amp; hdu 1443 Joseph(约瑟夫环变形)

    题目链接: POJ  1012: id=1012">http://poj.org/problem?id=1012 HDU 1443: pid=1443">http:// ...

  4. POJ 1012 Joseph 约瑟夫问题

    http://poj.org/problem?id=1012 早上去图书馆复习苦逼的复习....万恶的数逻.T T我还要自我安慰的说复习完了奖励回来刷水题~ 10点多的时候外面校运会大吼撑杆跳的那个. ...

  5. poj 1012 Joseph (约瑟夫问题)

    Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 47657   Accepted: 17949 Descript ...

  6. POJ 1012 Joseph 推导,暴力,约瑟夫环,打表 难度:2

    http://poj.org/problem?id=1012 答案以954ms飘过,不过这道题可以轻松用打表过 思路:如果我们把每个人位于数组中的原始编号记为绝对编号,每次循环过后相对于绝对编号为0的 ...

  7. poj 1012——Toseph

    提交地址:http://poj.org/problem?id=1012                                                                 ...

  8. POJ 1012 Joseph

    Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44650   Accepted: 16837 Descript ...

  9. POJ 1012 Joseph(打表题)

    题意:约瑟夫环的变形.要求寻找到一个杀人循环节m使后半节的坏人先被所有杀光. 直接链表模拟出结果,再打表即可: 代码:(凝视的是打表码) #include<iostream> #inclu ...

  10. POJ 1012:Joseph

    Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 50068   Accepted: 19020 Descript ...

随机推荐

  1. R-CNN算法概要

    参考论文:Rich feature hierarchies for accurate object detection and semantic segmentation 下载地址:https://a ...

  2. 双刃剑MongoDB的学习和避坑

    双刃剑MongoDB的学习和避坑 MongoDB 是一把双刃剑,它对数据结构的要求并不高.数据通过key-value的形式存储,而value的值可以是字符串,也可以是文档.所以我们在使用的过程中非常方 ...

  3. ASP.NET Core 2.0 : 系列目录

    目录: ASP.NET Core 2.0 : 一. 概述 ASP.NET Core 2.0: 二. 开发环境 ASP.NET Core 2.0 : 三. 项目结构 ASP.NET Core 2.0 : ...

  4. vs不支持通过afxgetmainwnd()获取窗口句柄(转)

    问题: 在vc6中这样代码顺利通过,可执行 ::SetDlgItemText(AfxGetMainWnd()-> m_hWnd,IDC_TIME,strTime); (这是在对话框程序中,代码在 ...

  5. PyCharm运行报编码错误

    运行报如下错误: SyntaxError: Non-ASCII character '\xe8' in file /home/ubuntu/code/201803091253-text.py on l ...

  6. RobotFramework自动化测试框架-移动手机自动化测试Get Network Connection Status和Set Network Connection Status关键字的使用

    Get Network Connection Status关键字用来获取手机的网络连接状态.在获取到连接状态后,会返回不同的数字. Set Network Connection Status关键字用来 ...

  7. [JZOJ5522] 图

    题目大意: 一个有向图,图中有\(n\)个点\(m\)条边且无重边无自环, 每秒第\(i\)条边出现的概率是\(\frac{p[i]}{100}\), 一开始\(Samjia\)在\(1\)点,每一秒 ...

  8. 洛谷2709 小B的询问(莫队)

    题面 题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R] ...

  9. [BZOJ1926][SDOI2010]粟粟的书架

    BZOJ Luogu Description 幸福幼儿园 B29 班的粟粟是一个聪明机灵.乖巧可爱的小朋友,她的爱好是画画和读书,尤其喜欢 Thomas H. Cormen 的文章.粟粟家中有一个 R ...

  10. wget命令企业级应用参数详解

    wget -O /etc/yum.repos.d/CentOS-Base.repo --spider: 爬虫,检查网站是不是好的 -T: 指定超时时间 --tries=2  指定重试的次数 -q   ...