64,639
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
#define OK 1
typedef struct DuLNode{
int num;
int pw;
struct DuLNode *next;
}DuLNode;DuLNode *L;;
void CreateList_L(int n)
{
DuLNode *p, *q;
L=(DuLNode*)calloc(1, sizeof(DuLNode));
L->num=1;
p=L;
cout<<n<<"个人的密码:";
cin>>p->pw;
for(int i =2; i<=n;i++){
q=(DuLNode*)malloc( sizeof(DuLNode));
q->num=i;
cin>>q->pw;
p->next=q;
p=q;
}
p->next=L;
}
int GetElem_L(int i){
DuLNode *p,*q;
p=L;
while(i>=1){
for(int j=2;j<=i;j++)
{
q=p;
p=p->next;
}
q->next=p->next;
i=p->pw;
cout<<p->num<<" ";
free(p);
p=q->next;
}
return OK;
}
void main()
{
int i,n;
cout<<"输入创建人数:";
cin>>n;
CreateList_L(n);
cout<<"输入极限值:";
cin>>i;
cout<<"输出序号:";
GetElem_L(i);
}
//假设有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();
}
int GetElem_L(int i){
DuLNode *p,*q;
p=L;
while(i>=1){
for(int j=2;j<=i;j++)
{
q=p;
p=p->next;
}
q->next=p->next;
i=p->pw;
cout<<p->num<<" ";
free(p);
if (p==q) break;//加
p=q->next;
}
return OK;
}