求一C写的约瑟夫环问题的程序!

bjchp 2005-03-23 11:26:25
求一C写的约瑟夫环问题的程序!
先谢了!
...全文
292 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
bjchp 2005-03-26
  • 打赏
  • 举报
回复
再次谢谢各位大虾的帮助!
这是我写的:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * link;
}*list,*p,*r;
void JOSEPHUS(int n,int m,int k);
main()

{
int n,m,k;
printf("请输入人数:");
scanf("%d",&n);
printf("\n请输入从哪里开始:");
scanf("%d",&k);
printf("\n请输入数到:");
scanf("%d",&m);
if(n!=0)
JOSEPHUS(n,m,k);
system("PAUSE");
return 0;
}
void JOSEPHUS(int n,int m,int k)
{
int i;
for (i=1;i<=n;i++){
p=(struct node * )malloc(sizeof(struct node));
p->data=i;
if(list==NULL)
list=p;
else
r->link=p;
r=p;
}
p->link=list;
p=list;
for (i=1;i<k;i++){
r=p;
p=p->link;
}
while(p->link!=p){
for(i=1;i<m;i++){
r=p;
p=p->link;
}
r->link=p->link;
printf("%d",p->data);
free(p);
p=r->link;
}
printf("\n最后被删除结点是%d\n",p->data);
}
ChenFengqing 2005-03-25
  • 打赏
  • 举报
回复
强人好多的啊,就是不知道有没有人灌水,呵呵!
llf_hust 2005-03-25
  • 打赏
  • 举报
回复
#include "stdio.h"
#include "stdlib.h"
typedef struct _list
{
int val;
struct _list* next;
}List,* pList;


void main()
{
int n,i;
pList head,temp,cur,pre,flw;

scanf("%d",&n);

head =(pList) malloc(sizeof(List));
head->val = 1;
cur = head;

for(i = 2 ; i <= n ; i++)
{
temp = (pList) malloc(sizeof(List));
temp->val = i;
cur->next = temp;
cur = temp;
}

cur->next = head;
pre = cur;
cur = head;

i = 1;
while(cur->next != cur)
{
if(i%3==0)
{
pre ->next = cur->next;
free(cur);
cur = pre->next;
}
else
{
pre =cur;
cur= cur->next;
}

i++;

}

printf("%d\n",cur->val);
free(cur);
}


我也写了一个
shisan107 2005-03-25
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct people)
struct people
{int num;
int sec;
struct people *next;
};
int n;
struct people *creat(void)
{struct people *head,*p1,*p2;
int i;
head=p1=p2=(struct people*)malloc(LEN);
p1->num=1;
scanf("%d",&p1->sec);
for(i=2;i<=n;i++)
{p1=(struct people*)malloc(LEN);
p2->next=p1;
p2=p1;
p1->num=i;
scanf("%d",&p1->sec);
}
p2->next=head;
return(head);
}
del(struct people *head,int x)
{struct people *p1,*p2;
int i,s,j;
s=x;
p1=p2=head;
for(i=1;i<n;i++)p1=p1->next;
for(i=1;i<n;i++)
{for(j=1;j<=s;j++)
{p2=p1;p1=p1->next;}
p2->next=p1->next;
s=p1->sec;
printf("The %dth people be deleted.The secret is %d.\n",p1->num,p1->sec);
free(p1);
p1=p2;
}
printf("The last one is the %dth people.\n",p1->num);
}
main()
{struct people *head;
int x;
printf("Please input there are how many people:");
scanf("%d",&n);
printf("please input their secrets:\n");
head=creat();
printf("Who is the first be deleted:");
scanf("%d",&x);
del(head,x);
}

大一时编的,现在看着真不舒服.其实不难,楼主自己应该好好想想,肯定能做出来的.
zengweipeng 2005-03-25
  • 打赏
  • 举报
回复
呵呵,我也来一个:
void joseephus(Type a[],int n,s,m)
{
int i,j,k,tmp;
if(m==0)
{
cerr<<"m=0 error!"<<endl;
return;
}
for (i=0;i<n;i++) a[i]=i+1;
i=s-1;
for(k=n;k>=1;k--)
{
if(i==k) i=0;
i=(i+m-1)%k;
if(i!=k-1)
{
tmp=a[i];
for(j=i;j<k-1;j++) a[j]=a[j+1];
a[k-1]=tmp;
}
}
for(k=0;k<n/2;k++)
{
tmp=a[k];
a[k]=a[n-k-1];
a[n-k-1]=tmp;
}
}
////////////////
stupidfish2004 2005-03-25
  • 打赏
  • 举报
回复
内存超作将OUT开始的M个空间置为0
zhousqy 2005-03-24
  • 打赏
  • 举报
回复
来一个不用链表的#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
int m, n, step=0, index=0, cir=0, t;
scanf("%d%d", &m, &n);
char *out = (char*)malloc(m);
if(!out) return -1;
memset(out, 0, m);
while(1)
{
if(step==m) break;
while(cir<n)
{
if(!out[index]) ++cir;
index=(index+1)%m;
}
cir=0; ++step; out[(index+m-1)%m]=1;
printf("\nNo.%d out.", !index?m:index);
}
free(out);
system("PAUSE");
return 0;
}
========
好短
SailorK 2005-03-24
  • 打赏
  • 举报
回复
这么旺!我就不显丑了:0)
vikinzqs 2005-03-24
  • 打赏
  • 举报
回复
刚学C时写的,写得比较臭,自己参考一下可以写出更好更简洁的!
--------------------------

#include<stdio.h>
#include<alloc.h>
#include<stdlib.h>

#define MAX 30
typedef struct Node{
int num;
int code;
struct Node *next;
}LNode,*linklist;
void input(int &x,int &y);
void creatdel(int &a,int &b);
int code[MAX];
void main(){
int logic=0;
int m=0,n=0;
while(logic!=2){
printf(" 1. start\n");
printf(" 2. exit\n");
printf("please select the \"start\" or \"exit\"\n");
select:
scanf("%d",&logic);
if(logic!=1&&logic!=2){
printf("select error,try it again! 1 or 2\n");
goto select;
}
if(logic==2){
break;
}
input(m,n);
creatdel(m,n);
}
}
void input(int &x,int &y){
input:
printf("please input the \"m\",\"n\"(n<=30)\n");
scanf("%d,%d",&x,&y);
if(y>30){
printf
("input error\n");
goto input;
}
printf("please input the code\n");
for(int i=1;i<=y;i++){
scanf("%d",&code[i]);
}
}
void creatdel(int &a,int &b){
linklist L=NULL;
linklist p=NULL;
linklist q=NULL;
L=(linklist)malloc(sizeof(LNode));
L->num=1;
L->code=code[1];
L->next=NULL;
for(int i=2;i<=b;i++){
p=(linklist)malloc(sizeof(LNode));
p->num=i;
p->code=code[i];
p->next=NULL;
if(i==2){
L->next=p;
q=p;
}
else{
q->next=p;
q=q->next;
}
}
p->next=L;
p=L;
div_t tem=div(a,b);
int temp=tem.rem;
for(i=2;i<temp;i++){
p=p->next;
}
q=p->next;
a=q->code;
b--;
printf("the member is exit %d\n",q->num);
p->next=p->next->next;
free(q);
for(int r=b-1;r>=0;r--){
div_t tem=div(a,b);
int temp=tem.rem;
for(i=1;i<temp;i++){
p=p->next;
}
q=p->next;
a=q->code;
b--;
printf("the member is exit %d\n",q->num);
p->next=p->next->next;
free(q);
}
}
losky 2005-03-24
  • 打赏
  • 举报
回复
我也来给一个,是规定用单循环链表写的::
#include<stdio.h>
struct man
{
int num;
int state;
struct man *next;
};
struct man *creat()
{
struct man *head;
head=(struct man*)malloc(sizeof(struct man));
head->next=NULL;
head->num=1;
head->state=0;
return (head);
}
int link(struct man *head,int n)
{
struct man *p,*s;
int i;
p=head;
for(i=1;i<n;i++)
{
s=(struct man*)malloc(sizeof(struct man));
p->next=s;
p=s;
p->num=i+1;
p->state=0;
}
p->next=head;
return 1;
}
main()
{

int n,i,j,m;
char c;
struct man *h,*p,*s;
do
{
h=creat();
printf("输入总人数\n");
scanf("%d",&n);
if(link(h,n)!=1)
printf("内存不足\n");
s=h;
p=(struct man*)malloc(sizeof(struct man));
p->next=s;
printf("输入一个数,当有人报到这个数时就出列\n");
scanf("%d",&m);
printf("\n下面是出列的人的顺序\n");
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
{
p=p->next;
if(p->state==1)
i--;
}
printf("%d\n",p->num);
p->state=1;
}
printf("想继续测试输入按y\n");
getchar();
scanf("%c",&c);
}while(c=='y');

}
galanz 2005-03-24
  • 打赏
  • 举报
回复
mark
liujingfu123 2005-03-24
  • 打赏
  • 举报
回复
/**********************************************
********* Josephus problem ************
*********************************************/
#include <malloc.h>
#include <stdio.h>
struct boy /* struct of boy*/
{
int num;
struct boy *next;
};
int main()
{
int i;
int number; /* 小孩的数目 */
int interval; /* 即报数从1- interval */
struct boy *head, *p, *current;

printf("please input the number of boys:");
scanf("%d", &number);
printf("please input the interval:");
scanf("%d", &interval);
if(number < interval || number <= 0 || interval <= 0)
{
printf("input error!!!\n");
return 0;
}
head = (struct boy *) malloc(sizeof(struct boy));
head->num = 1;
head->next = NULL;
current = head;
printf("Boy numbers:%d,", head->num);
for (i = 2; i <= number; i++) /* 建立单向循环链表 */
{
p = (struct boy *) malloc(sizeof(struct boy));
p->num = i;
p->next = NULL;
current->next = p;
current = p;
printf(" %d,", p->num);
}
printf("\n");
current->next = head;
current = head;
printf("Out queue order:");
while (current->next != current) /*loop to find the winner */
{
for (i = 1; i < interval; i++) /* 查找报数为interval的小孩结点 */
{
p = current;
current = current->next;
}
p->next = current->next; /* 删除报数为interval的小孩结点 */
printf(" %d,", current->num);
free(current); /* 释放报数为interval的小孩结点空间 */
current = p->next;
}
printf("\n");
printf("No.%d boy's won!\n", current->num);
return 0;
}
zengwujun 2005-03-24
  • 打赏
  • 举报
回复
学习
pcboyxhy 2005-03-24
  • 打赏
  • 举报
回复
来一个不用链表的#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
int m, n, step=0, index=0, cir=0, t;
scanf("%d%d", &m, &n);
char *out = (char*)malloc(m);
if(!out) return -1;
memset(out, 0, m);
while(1)
{
if(step==m) break;
while(cir<n)
{
if(!out[index]) ++cir;
index=(index+1)%m;
}
cir=0; ++step; out[(index+m-1)%m]=1;
printf("\nNo.%d out.", !index?m:index);
}
free(out);
system("PAUSE");
return 0;
}


stupidfish2004 2005-03-24
  • 打赏
  • 举报
回复
用循环链表
ljwxsd 2005-03-24
  • 打赏
  • 举报
回复
#include<stdio.h>
#define new(type) (type *)malloc(sizeof(type))
typedef struct rNode * RNodePtr;
typedef struct rNode {
int pos;
RNodePtr next;
}RNode,* RingList;
void InitRing(int n,RingList *R)
{ RNodePtr p,q;
int i;
*R=q=new(RNode); //创建有n个结点的循环单链表
for(i=1;i<n;i++)
{ p=new(RNode);
q->pos=i;
q->next=p;
q=p;
}
p->pos=n;
p->next=*R;
*R=p;//R指向循环链表表尾结点,作计数准备
}
void DeleteDeath(int n,int k,RingList *R)
{ int i,j;
RNodePtr p,q;
p=*R;
for (i=1;i<=n/2;i++) //删除一半结点
{ for(j=1;j<=k-1;j++) //沿链前进k-1步
p=p->next;
q=p->next; //q为被删结点
p->next=q->next; //删除
printf("%4d",q->pos); //输出被删除的结点的位置
free(q);
}
*R=p;
printf("\nfinished.\n");
}
main()
{ RingList R;
RNodePtr p,q;
int n,k;
printf("n,k=");
scanf("%d%d",&n,&k);
InitRing(n,&R);
DeleteDeath(n,k,&R);
}
losky 2005-03-24
  • 打赏
  • 举报
回复
memset(out, 0, m);

这是个什么函数?
greenteanet 2005-03-24
  • 打赏
  • 举报
回复
看看谭浩强的C程序设计里面的习题解释/

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧