#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int *JosephWithArray(int n)
{
int *p;
p = new int[n];
int i;
for(i=0;i<n;i++)
{
p[i]=i+1;
}
return p;
}
int NextPeople(int prev,int *WorkingArray,int number_of_people,int step)
{
int count=0;
int travel=prev;
int emptyplace=0;
while(count<step)
{
travel=(travel+1)%number_of_people;
if(WorkingArray[travel])
{
count++;
emptyplace=0;
}
else
{
emptyplace++;
if(emptyplace==number_of_people)
{
return -1;
}
}
}
return travel;
}
void CreatOutput(int *WorkingArray,int number_of_people,int step)
{
int i;
int outnum=-1;
for(i=0;i<number_of_people;i++)
{
outnum=NextPeople(outnum,WorkingArray,number_of_people,step);
assert(outnum>-1 && outnum<number_of_people);
printf("%d",WorkingArray[outnum]);
WorkingArray[outnum]=0;
}
printf("\n");
}
void Dispose(int *WorkingArray)
{
delete[] WorkingArray;
}
int main()
{
int n,m;
int *WorkingArray;
printf("input number of people:");
scanf("%d",&n);
printf("input step:");
scanf("%d",&m);
WorkingArray = JosephWithArray(n);
CreatOutput(WorkingArray,n,m);
Dispose(WorkingArray);
return 0;
}
Compiling...
1234.c
F:\测试代码\1234.c(8) : error C2065: 'new' : undeclared identifier
F:\测试代码\1234.c(8) : warning C4047: '=' : 'int *' differs in levels of indirection from 'int '
F:\测试代码\1234.c(8) : error C2143: syntax error : missing ';' before 'type'
F:\测试代码\1234.c(9) : error C2143: syntax error : missing ';' before 'type'
F:\测试代码\1234.c(10) : error C2065: 'i' : undeclared identifier
F:\测试代码\1234.c(55) : error C2065: 'delete' : undeclared identifier
F:\测试代码\1234.c(55) : error C2059: syntax error : ']'
执行 cl.exe 时出错.
1234.obj - 1 error(s), 0 warning(s)
看看这是怎么回事尽量说详细点