文章标题
2015-12-03 14:51
459 查看
Josephus problem
Josephus problemProblem Description
Solution
General caseRecursion formula
Special casem 2
Reference1
Problem Description
Solution
General case:Recursion formula
Suppose we number the position from 0 to n−1g(n,m)=(g(n−1,k)+k)mod n,with g(1,k)=0
This is my codes:
#include <iostream> using namespace std; int josephus_Recursion(int n,int m) { if(n == 1) return 0; else return (josephus_Recursion(n-1,m)+m)%n; } int main() { int n,m; cin >> n >> m; cout << josephus_Recursion(n,m); }
Special case:m = 2
Suppose we number the position from 1 to mJ(2n)=2J(n)−1
J(2n+1)=2J(n)+1
So,in general
J(n)=2(J(n)+n
This is my codes:
#include <iostream> using namespace std; int josephus_Special(int n,int m=2) { if (n==1) return 1; else return 2*(josephus_Special(n/2)+n%2)-1; } int main() { int n,m; cin >> n >> m; cout << josephus_Special(n,m)<<endl; return 0; }
Reference1
https://en.wikipedia.org/wiki/Josephus_problem ↩相关文章推荐
- Java操作Excel
- vs2010 打开 vs2012 的解决方案(汇总)
- springMVC 拦截器如何做登录检查及页面跳转
- git基础学习
- Unity 通过 www 下载 assetbundle , 在 iOS9 设备无法下载的问题
- 经过各种坑之后centos+ uwsgi + nginx +django 终于配好了
- OpenExplore文件插件
- Objective C中数组排序几种情况的总结
- The VMware Authorization Service is not running.
- gulp的流与执行顺序
- CSDN网站系统升级公告
- 给 Android 开发者的 RxJava 详解
- Android Studio 无法启动 Error launching Android Studio
- Integer.getInteger()与Integer.parseInt()的区别
- smarty框架中 点击即可编辑标题的 jquery实现代码
- 一个form表单包含多个submit提交动作
- Mattt大神写单例用的代码片段
- Android Layouts
- 实用教程:如何着色更高效
- 主流编程语言简介