链表:
#include<stdio.h>
#include<malloc.h>
typedef struct node{
int info;
struct node *next;
};
node *tbuildhlink(int n) /*带头节点的尾插法*/
{
node *head,*s,*p2;
int i=1;
head=(node *)malloc(sizeof(node));
p2=head;
while(i<=n)
{
s=(node *)malloc(sizeof(node));
s->info=i;
p2->next=s;
p2=s;
i++;
}
if(p2) p2->next=head;
return(head);
}
void Display(struct node* head)
{
node *p;
p=head->next;
if(!p)
{
printf("\nthe hlink is empty!");
}
else
{
printf("\nthe value of the hlink is:\n");
while(p!=head)
{
printf("%d--->",p->info);
p=p->next;
}
}
printf("^\n");
}
int delete_node(struct node *head,int n,int m)
{
int count=1,sum=n;
struct node *p,*pre;
pre=head;
p=pre->next;
while(sum>1)
{
if(p==head)
{
p=p->next;
}
if(count<m)
{
pre=p;
p=p->next;
count++;
}
if(count==m)
{
if(p==head)
{
p=p->next;
}
printf("第%d个人出列.\n",p->info);
pre->next=p->next;
free(p);
p=pre->next;
count=1;
sum--;
}
}
return(pre->info);
}
int main()
{
node *head;
int n,m;
printf("输入n,m:");
scanf("%d%d",&n,&m);
head=tbuildhlink(n);
Display(head);
printf("最后剩下第%d个.\n",delete_node(head,n,m));
return(0);
}
写了,大家,但我觉得是满难的,又没有简单的方法啊
有没有简单一点的方法啊
我还没看懂题呢
m不就是最后一个人吗?
#include "stdio.h"
#define n 100
#define m 10
void main()
{int i,sum=0,a[101];
int t=0;
for(i=1;i<=100;i++)
a[i]=1;
for(i=1;i<=100;i++)
{
sum=(sum+a[i])%m;
if(sum==0&&a[i]==1)
{ t++;
a[i]=0;
if(t==100)
{ printf("%d",i);
break;
}
}
if(i==100)i=0;
}
}