64,661
社区成员
发帖
与我相关
我的任务
分享
//假设有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();
}
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int a;
int b;
struct node *next;
}Lnode;
Lnode *creatlist(int n);
void printlist(Lnode *p);
void fun(Lnode *p,int n,int m);
main()
{
Lnode *p;
int n,m;
printf("input n and m:\n");
scanf("%d,%d",&n,&m);
p=creatlist(n);
printf("the list is:\n");
printlist(p);
printf("\nchu lie sunxu:\n");
fun(p,n,m);
}
Lnode *creatlist(int n)
{ int i;
Lnode *head,*p,*s;
head=( Lnode *) malloc(sizeof(Lnode ));
p=head;
head->next=p;
printf("enter a,b:\n");
scanf ("%d,%d",&p->a,&p->b);
for (i=2; i<=n; ++i )
{ s=(Lnode *) malloc(sizeof(Lnode ));
scanf ("%d,%d",&s->a,&s->b);
s->next=head;
p->next=s; p=s;
}
return (s);
}
void printlist(Lnode *p)
{ Lnode *q;
q=p->next;
while(q!=p)
{ printf("%3d",q->b);
q=q->next;
}
printf("%3d",p->b);
}
void fun(Lnode *p,int n,int m)
{ Lnode *q;
int i,count;
q=p;
for(i=1;i<n;i++)
{
count=0;
if(i!=1)
count++;
while(count!=m)
{
p=q;
q=q->next;
count++;
}
printf("%3d",q->a);
m=q->b;
p->next=q->next; free(q);
q=p->next;
}
printf("%3d",p->a);
}