64,648
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int _data){
data=_data;
next=NULL;
}
};
class Linklist{
private:
Node* head;
public:
Linklist(){
head=NULL;
}
void insert(Node *node, int index) {
if (head == NULL) {
head = node;
head->next = head;
return;
}
if (index == 0) {
node->next = head->next;
head->next = node;
return;
}
Node *current_node = head->next;
int count = 0;
while (current_node != head && count < index - 1) {
current_node = current_node->next;
count++;
}
if (count == index - 1) {
node->next = current_node->next;
current_node->next = node;
}
if (node == head->next) {
head = node;
}
}
void delete_node(int index)
{
if(head==NULL)
{
return;
}
Node* current_node=head;
int count=0;
if(index==0)
{
head=head->next;
delete current_node;
return;
}
while(current_node->next != NULL && count<index-1)
{
current_node=current_node->next;
count++;
}
if(count==index-1 && current_node->next != NULL)
{
Node* delete_node=current_node->next;
current_node->next=delete_node->next;
delete delete_node;
}
}
int get_len(Node *head)
{
Node *p=head->next;
int len=0;
while ( p )
{
len++;
p=p->next;
}
return len; //返回长度;
}
void output_node(){
if(head==NULL){
return;
}
Node* current_node=head;
int count=-1;
int length = get_len(head);
while(current_node!=NULL){
count++;
if(count == length/2){
cout << current_node->data;
}
current_node = current_node->next;
}
}
};
int main(){
Linklist linklist;
int n,m,k;
cin>>n>>m;
for (int i = 1; i <= n; i++) {
Node *node = new Node(i);
linklist.insert(node, i - 1);
}
for(int i=1;i<=m;i++){
cin>>k;
linklist.delete_node(k-1);
}
linklist.output_node();
return 0;
}
//假设有n个人团团围做,从第1个人开始数数,数到第m个人时候,第m个人出列,
//然后继续从1开始数数,数到第m个人退出
#include <stdio.h>
#include <conio.h>
int i,k,t;
int n,m;
static char f[1001];//0该座位未出圈,1该座位已出圈
void main() {
while (1) {
printf("Input n m(1000>=n>=m>=1):");
fflush(stdout);
rewind(stdin);
if (2==scanf("%d%d",&n,&m)) {
if (1000>=n && n>=m && m>=1) break;
}
}
t=0;//已出圈总人数
i=1;//座位编号
k=1;//当前要数的数
while (1) {
if (0==f[i]) {
if (m==k) {
t++;
f[i]=1;
printf("%3d ",i);
if (0==t%10) printf("\n");
if (t>=n) break;
}
k++;if (k>m) k=1;
}
i++;if (i>n) i=1;
}
cprintf("Press any key ...");
getch();
}