`
445822357
  • 浏览: 737788 次
文章分类
社区版块
存档分类
最新评论

(顺序表应用5.1.1)POJ 3750 小孩报数问题(基本的约瑟夫环问题:给出人数n,出发位置w,间隔数s)

 
阅读更多
/*
 * POJ_3750.cpp
 *
 *  Created on: 2013年10月30日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>


using namespace std;

const int maxn = 70;

int main(){
	char name[maxn][maxn];//小孩名字
	int p[maxn];//小孩序号

	int n;
	scanf("%d",&n);

    int i;
    for(i = 0 ; i < n ; ++i){
    	p[i] = i;
    	scanf("%s",&name[i]);
    }

    int w,s;
    scanf("%d,%d",&w,&s);

    w = (w + n - 1 )%n;//出发位置

    do{
    	w = (w + s - 1)%n;//出列位置

    	cout<<name[p[w]]<<endl;

    	int j;
    	for(j = w ; j < n - 1 ; ++j){//将出列位置后元素向前移动
    		p[j] = p[j+1];
    	}
    }while(--n);


}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics