链接:http://www.lightoj.com/volume_showproblem.php?problem=1179

给你一个n个人的圈  每次取出第m个人  问最后一个人是多少号  利用递归直接有每次出来的人

    for(int i = 2; i <= n; i++)  ///这是for循环的求法
            last = (last + k) % i;  
        printf("Case %d: %d\n", kcase++, last+1);  

 

#include<cstdio>///还是喜欢递归
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<math.h>
#include<string>
#include<map>
#include<vector>
using namespace std;
#define LL long long
#define N 100006
int Q(int n,int m,int v)
{
    if(v==1) return (n+m-1)%n;
    else return (Q(n-1,m,v-1)+m)%n;
}
int main()
{
    int T,t=1;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);//后一个n便是第n次取出的人 可以是任意数字
        printf("Case %d: %d\n",t++,Q(n,m,n)+1);
    }
    return 0;
}

 

posted on 2017-10-17 15:46  云胡不喜。  阅读(172)  评论(0编辑  收藏  举报