约瑟夫循环 升级版

输入n,代表总人数
第二行输入每个人的编号,按照编号进行循环,即用编号代替了原先的定值。
最后输出幸存者的编号。

#include<stdio.h>
int main(){
	int i,j,k,m,n;
	i=j=k=m=n=0;
	int peo,where;
	scanf("%d",&peo);
	int all[peo];
	for(i=0;i<peo;i++){
		scanf("%d",&all[i]);
	}
	where=0;
	while(peo!=1){
		where=(where+all[where])%peo;
		if(where==peo-1){
			peo--;continue;
		}
		else{
			while(where!=peo-1){
				all[where]=all[where+1];
				where++;
			}
			peo--;continue;
		}
	}
	printf("%d\n",all[0]);
}
发布了19 篇原创文章 · 获赞 8 · 访问量 340

猜你喜欢

转载自blog.csdn.net/bupt_sanqing/article/details/104259986