站外题求助
  • 板块灌水区
  • 楼主封禁用户
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/9/1 17:15
  • 上次更新2024/9/1 21:29:46
查看原帖
站外题求助
973480
封禁用户楼主2024/9/1 17:15

题目

#include<bits/stdc++.h>
using namespace std;
typedef struct DNode{
  int data;
  struct DNode *pre,*next;
}DNode;
DNode *phead;
void headinsert(int x){
  DNode *pTemp=(DNode *)malloc(sizeof(DNode));
  pTemp->data=x;
  pTemp->next=phead->next;
  pTemp->pre=phead;
  phead->next->pre=pTemp;
  phead->next=pTemp;
  return;
}
void printlist(){
  DNode *p=phead->next;
  while(p!=phead){
    cout << p->data << " ";
    p=p->next;
  }
  cout << endl;
  return;
}
void detele(){
  DNode *q=phead->next;
  phead->next=q->next;
  q->next->pre=phead;
  free(q);
  return;
}
void tailinsert(int x){
  DNode *p=phead->next;
  while(p->next!=phead) p=p->next;
  DNode *pTemp=(DNode *)malloc(sizeof(DNode));
  pTemp->data=x;
  pTemp->next=p->next;
  p->next=pTemp;
  return;
}
int main(){
  phead=(DNode *)malloc(sizeof(DNode));
	phead->next=phead;
  phead->pre=phead;
  int n,k;
  cin >> n >> k;
  for(int i=n;i>=1;i--) headinsert(i); 
  int dir=1,cnt=0;
  DNode *p=phead->next;
  while(phead->next->next!=phead){
    cnt++;
    if(cnt==k){
      detele();
      cnt=0;
    }else{
      if(dir==1) tailinsert(p->data);
      else headinsert(p->data);
      detele();
    }
    if((p->pre==phead&&dir==-1)||(p->next==phead&&dir==1)){
      dir=-dir;
    }else{
      if(dir==1) p=p->next;
      else p=p->pre;
    }
  }
  cout << phead->next->data;
  return 0;
}
2024/9/1 17:15
加载中...