2
20
40
Sample Output
1 7 19
1 19 37
链表的操作
#include
#include
using namespace std;
int main()
{
int t,n;
cin >> t;
while(t--)
{
cin>>n;
int k;
k=2;
listblist;
list::iterator it;
for(int i=1;i<=n;i++)
{
blist.push_back(i);
}
while(blist.size()>3)
{
int num;
num = 1;
for(it = blist.begin();it !=blist.end();)
{
if(num++%k==0)
{
it=blist.erase(it);
}
else
{
it++;
}
}
if(k==2)
{
k=3;
continue;
}
else
{
k=2;
continue;
}
}
for(it=blist.begin();it!=blist.end();it++)
{
if(it!=blist.begin())
{
cout<
}
cout<
}
cout << endl;
}
return 0;
}
不用链表库,自己写结构体
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef struct _node{
int value;
struct _node *next;
} Node;
typedef struct _list{
Node* head;
}List;
void addl(List *pList,int number){
Node *p=(Node*)malloc(sizeof(Node));
p->value=number;
p->next=NULL;
Node *last=pList->head;
if(last){
while(last->next){
last=last->next;
}
last->next=p;
}else{
pList->head=p;
}
}
int list_size(List *pList){
int _size;
_size=0;
Node *p;
for(p=pList->head;p;p=p->next){
_size++;
}
return _size;
}
void print(List *pList){
Node *p;
for(p=pList->head;p;p=p->next){
if(p!=pList->head){
printf(" ");
}
printf("%d",p->value);
}
printf("\n");
}
int main(){
int t,n;
cin>>t;
while(t--){
cin>>n;
int k;
k=2;
List blist;
blist.head=NULL;
for(int i=1;i<=n;i++){
addl(&blist,i);
}
while(list_size(&blist)>3){
int num;
num=1;
Node *q;
q=NULL;
for(Node *p=blist.head;p;q=p,p=p->next){
if(num++%k==0){
if(q){
q->next=p->next;
}else{
blist.head=p->next;
}
free(p);
}
}
k==2 ? k=3:k=2;
}
print(&blist);
}
return 0;
}
为什么和别人的代码一样?别问!问就是灰灰考研学的!
标签:Node,head,48,int,C语言,next,blist,include,队列训练
来源: https://www.cnblogs.com/FantasticDoubleFish/p/14381883.html