c++版约瑟夫问题,有点瑕疵
#include<iostream>struct link
{
char date;
link *next;
};
class yese
{
public:
yese(int x);
~yese();
void play();
private:
link *dat;
};
yese:: yese(int x)
{
link *fp,*team;dat=NULL;
for(int i=1;i<=x;i++)
{
fp=new link;
fp->date=i;
if(dat==NULL)
{
dat=fp;fp->next=dat;
}
else
{
team->next=fp;
fp->next=dat;
}
team=fp;
}
dat=team;
}
yese:: ~yese()
{
delete dat;
dat=NULL;
}
void yese:: play()
{
link *fp;
while(dat->next!=dat)
{
fp=dat->next->next;
dat=fp ->next;
fp->next=dat->next;
std::cout<<(int)dat->date<<"->";
delete dat;
dat=fp;
}
}
int main()
{
yese k(16);
k. play();
return 0;
}
页:
[1]