void init(int *Ren,int RenShu)
{
int i;
for (i=0;i<RenShu;i++)
{
*(Ren+i)=1;
}
}
void main()
{
//////////////////
int i=0;
int j=0;
int *Ren;
int RenShu=100;
int JiShu=0;
/////////////////////
Ren=(int *)malloc(RenShu*sizeof(int));
init(Ren,RenShu);
while (1)
{
if (i==RenShu) i=0;
if (*(Ren+i)==1)
{
if (++JiShu % 3==0)
{
*(Ren+i)=0;
j++;
if (j==RenShu-1) break;
}
}
i++;
}
for (i=0;i<RenShu;i++)
{
printf("Ren[%d]=%d,",i,*(Ren+i));
}
free(Ren);
//printf("pai[%d]=%d,",j,pai[j]);
}
我给你c的吧,其实很简单的啊,就是约瑟夫问题(猴子选大王的问题),用链表解决的,任意个元素,任意号码(小于总数)开始删除,输出了每个被删除的号码
输入的时候用逗号间隔,前面的是总数,后面的是循环因子
/*
*The monkey king problem
*By yuxh312
*
*/#include<stdio.h>
#include<stdlib.h>
typedef int DataType;
void Selete(int n,int m)
{
int i,j;
Node front,*pre,*ptr;
SetNode(&front); /*use linklist struct script 'n' nummber monkeys*/
ptr=&front;
for(i=1;i<=n;i++)
{
InsertAfter(ptr,i);
ptr=NextNode(ptr);
}
pre=&front; /*let pointers indicate head node and start node*/
ptr=NextNode(pre);
for(i=1;i<=n-1;i++)
{
for(j=1;j<=m-1;j++) /*pointers step 'm-1' locations ,during this ,consider the linklist*/
{ /* as a circle ,if pointer face tail,that is 'ptr=NULL',*/
pre=ptr;
ptr=NextNode(ptr); /*let 'pre' indicate head and let 'ptr' indicate start */
if(ptr==NULL)
{
pre=&front;
ptr=NextNode(pre);
}
}/*for j;*/
printf("Delete monkey %d\n",ptr->data);
ptr=NextNode(ptr);/*delete the node whinch pointer 'ptr' indicated,*/
DeleteAfter(pre);
if(ptr==NULL)/*if 'ptr' indicate "NULL",let "pre"and "ptr" indicate head and start*/
{
pre=&front;
ptr=NextNode(pre);
}
}/*for i;*/
printf("\n");
printf("Monkey %d is the king\n",ptr->data);
DeleteAfter(pre);/*free the last node*/
}
int main()
{
int n,m;
printf("\nInput n and m :");
scanf("%d,%d",&n,&m);
Selete(n,m);
void Josephus::game(int begin,int num) {
int location;
int i=0;
int temp=length;
while(length>1) {//当数组中的数不止一个时
if((begin+num)>length) location=begin+num-length-1;//确定开始的位置
else location=begin+num-1;
tmp[i]=array[location];//将满足条件的数从数组中剔除
i++;
for(int j=location;j<length-1;j++)//将该位置上的数剔除后,把后面的数往前移
array[j]=array[j+1];
length--;
begin=location;
}
tmp[i]=array[location-1];//数组中的最后一个数
length=temp;
}
void main() {
int number,start,many;
cout<<"Please enter the size of array:";
cin>>number;
cout<<"Please enter the start:";
cin>>start;
cout<<"Please enter the number:";
cin>>many;
Josephus jose(number);
jose.game(start-1,many);
jose.print();