51nod 1073【约瑟夫环问题】

N个人坐成一个圆环(编号为1 - N),从第1个人开始报数,数到K的人出列,后面的人重新从1开始报数。问最后剩下的人的编号。
例如:N = 3,K = 2。2号先出列,然后是1号,最后剩下的是3号。
Input
2个数N和K,表示N个人,数到K出列。(2 <= N, K <= 10^6)
Output
最后剩下的人的编号
Input示例
3 2
Output示例
3

约瑟夫问题详解:打开链接

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;
int yuesefu(int n,int m){
    if(n==1){
        return 0;
    }else{
        return (yuesefu(n-1,m)+m)%n;
    }
}
int main()
{
    int n,k;
    cin>>n>>k;
    cout<<yuesefu(n,k)+1<<endl;
    return 0;
}

posted @ 2018-06-14 14:56  神探小小狄  阅读(101)  评论(0编辑  收藏  举报
【推荐】 程序员网址大全  |  EF CodeFirst  |  百度地图.NET  |  MyNPOI  |  开源  |  我的皮肤  |  ASP.NET MVC4  |  前端提升   |  LINQ   |  WCF   |  EasyUI  | aaronyang 页脚html代码