POJ 1012 joseph
来源:互联网 发布:mac os安装windows 编辑:程序博客网 时间:2024/06/07 16:08
Joseph
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 54131 Accepted: 20667
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.
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,其实总人数编号为2*k,那么需要在0-k编号出列之前必须让所有[k,2*k]编号的人出列,需要找到这个循环数来让人出列。暴力求解,让m从k开始递增,对出列的数值进行判断,如果有出现出列数值小于k,重新开始循环
#include<iostream>#include<stdlib.h>using namespace std;int main(){ int k, s, j, m, n; int a[15] = {0};//存储k对应的循环变量m while (cin >> k) { if (k == 0) break; m = k; n = 2 * k; if (!a[k])//!a[k]==0 { while(1) { s = 0; for (j = 0; j <k; j++) { s = (s + m - 1) % (n - j); //s是每次删除的位置 if (s < k) break;//如果删除的是小于k的数值,那么就不符合题意,直接开始下一个m的判断 } if (j == k) break; m++; } a[k] = m; } cout << a[k] << endl; } return 0;}
0 0
- POJ 1012 Joseph
- POJ 1012 Joseph
- POJ 1012 Joseph
- POJ 1012 Joseph
- poj 1012 Joseph
- POJ 1012 Joseph
- poj 1012 Joseph
- 【POJ】1012 Joseph
- poj 1012 Joseph
- poj-1012 Joseph
- POJ 1012 Joseph
- poj 1012 Joseph
- Poj 1012 Joseph
- POJ - 1012 Joseph
- [POJ][1012]Joseph
- POJ 1012 Joseph
- POJ 1012 Joseph
- POJ 1012 Joseph(打表题)
- 手机成功访问node.js搭建的本地服务器
- 苹果审核(二)
- 数组链表区别
- 定时校正Linux服务器时间
- angular2中使用第三方js库
- POJ 1012 joseph
- (GIS可视化)ArcGis中属性连接、关联和空间连接的区别
- matlab之disparity函数实现
- Android三种播放视频的方式
- canvas实现简单直线扫描效果
- 【种类并查集】codeforces 505B Mr. Kitayuta's Colorful Graph
- #OSG+VS#第八周
- Android Studio如何快速生成get,set,tostring,构造函数
- Delphi TIniFile 用法