博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj-1012-约瑟夫问题
阅读量:6334 次
发布时间:2019-06-22

本文共 1548 字,大约阅读时间需要 5 分钟。

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

340

Sample Output

530 题目大意: 有k个好人和k个坏人,从第一个人开始循环报数(即报到m后从1开始报),报到m的人被处死,要求全部的坏人都处死,好人留下,求出m。
这个代码就是有点慢,所以我打了个表 #include
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]

  

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

  

转载地址:http://qusoa.baihongyu.com/

你可能感兴趣的文章
算法导论系列:分治算法
查看>>
16-Flutter移动电商实战-切换后页面状态的保持AutomaticKeepAliveClientMixin
查看>>
asp.net 生成静态页面
查看>>
2102: [Usaco2010 Dec]The Trough Game
查看>>
3212: Pku3468 A Simple Problem with Integers
查看>>
SVG 的使用
查看>>
网商银行×OceanBase:首家云上银行的分布式数据库应用实践
查看>>
ES 6大纲总结——Array的扩展
查看>>
用Pyenv 和 Virtualenv 搭建单机多版本 Python 虚拟开发环境
查看>>
esxi安装全过程及基本配置(转)
查看>>
排序--插入
查看>>
HTML简介
查看>>
CSS 盒子模型
查看>>
SpringApplicationRunListeners与ApplicationListener生命周期分析
查看>>
26. Intellij IDEA 启动项目ClassNotFoundException
查看>>
【SSH网上商城项目实战20】在线支付平台的介绍
查看>>
jsp页面随页面初始化加载js函数
查看>>
详解Document.Cookie
查看>>
Linux下第一次使用MySQL数据库,设置密码
查看>>
maven学习--1.安装与配置
查看>>