65,211
社区成员
发帖
与我相关
我的任务
分享/*输入n个小孩数,从s个小孩数起,数到m个,去掉一个小孩直剩下最后一个.*/
/*输入n个小孩数,从s个小孩数起,数到m个,去掉一个小孩直剩下最后一个.*/
#include <iostream>
using namespace std;
struct Node
{
int code;
Node *next;
};
int n;
int m;
int s;
Node *pcur=new Node;
Node *pivot=new Node;
int main()
{
bool getvalue();//判断数据正确性;
Node *creatnode();//创建一个链表
void countnode(int m);
void process();
if ( !getvalue() )
{
cout <<"你输入的数字不合法: ";
}
else
{
Node *pa=creatnode();
process();
delete []pa;
}
return 0;
}
bool getvalue()
{
cout <<"请输入小孩数: ";
cin>>n;
cout <<"请输入开始数: ";
cin>>s;
cout <<"请输入结束数: ";
cin>>m;
if ( (n >=2) && (m >=1 && m <n) && (s >=1 && s <n) )
{
return true;
}
else
{
return false;
}
}
void countnode(int m)
{
for (int i=1; i <m; i++)
{
pivot=pcur;
pcur=pivot->next;
}
}
Node *creatnode()
{
Node *px= new Node[n];
for (int i=1; i <=n; i++)
{
px[i-1].next = &px[i%n];
px[i-1].code = i;
cout <<px[i-1].code <<endl;
}
pivot->next= &px[n-1];
pcur->next= &px[n-1];
countnode(s-1);
return px;
}
void process()
{
for (int i=1; i <n; i++)
{
countnode(m);
static line=0;
cout <<pcur->code;
if ( (line++)%10 )
{
cout <<"\n";
}
pivot->next=pcur->next;
pivot=pcur;
}
}