poj 3750 小孩报数问题 约瑟夫问题

题意:有N个小孩围成一圈,给他们从1开始依次编号,现指定从第W个开始报数,报到第S个时,该小孩出列,然后从下一个小孩开始报数,仍是报到S个出列,如此重复下去,直到所有的小孩都出列(总人数不足S个时将循环报数),求小孩出列的顺序。

分析:约瑟夫问题

View Code
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
const int maxn =65;
string name[maxn];
int w , s , n;
int main() {
    scanf("%d",&n);
    for(int i=0;i<n;i++) cin>>name[i];
    scanf("%d,%d",&w,&s);
    int cnt = w - 1;
    for(int i=n;i>=1;i--) {
        cnt = ( cnt + s - 1) % i;
        cout<<name[cnt]<<endl;
        for(int j=cnt;j<i;j++) name[j] = name[j+1];
    }
    return 0;
}
posted @ 2012-07-05 15:26  lenohoo  阅读(176)  评论(0编辑  收藏  举报