约瑟夫问题-数组实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yilonglucky/article/details/50442884

约瑟夫问题_百度百科 http://baike.baidu.com/link?url=EuXtXV4hPi1AtRdzY0vMPG1gQg1kt3uCS5g4Bfy_RzWXAnDwwjJkhxWdRfk2LPnkqubRBTb52CXEaUa5jbIRaK


#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
int main(void)
{
	int total,out,start,i;
	int *Array;
	printf("please input the number of people:");
	scanf("%d",&total);
	printf("which one to be out:");
	scanf("%d",&out);
	Array=(int*)malloc(total*sizeof(int));

	for(i=0; i<total; i++){
		Array[i]=i+1;
		//printf("%5d",Array[i]);
	}
	//printf("\n");
	for(start=0; 1!=total; total--)
	{
		for(i=1; i<out; i++)
			start=(start+1)%total;
		//printf("%5d",Array[start]);
		for(i=start; i<total-1; i++)
			Array[i]=Array[i+1];
	}
	printf("\nthe last one is %d",Array[0]);
	system("pause");
	return 0;
}


没有更多推荐了,返回首页