此程序错在哪?(约瑟夫环)

zd3824812 2006-04-19 08:30:17
约瑟夫
#include<stdio.h>
#include<stdlib.h>

struct data{
int num;(输值时依次为1,2,3,4........)
int sec;
struct data *next;
}
main()
{struct data *result (struct data *head1);
struct data *prin (struct data *head2);
int n;int m=1;
struct data *head;
struct data *p1,*p2;
printf("please putin the count of your struct: ");
scanf("%d",&n);

while(m<=n)
{if(m==1) p2=head=p1=(struct data*)malloc(sizeof(struct data));(建链表)
else p1=(struct data*)malloc(sizeof(struct data));
p2->next=p1;
p2=p1;
printf("putin the data of present struct :\n ");
scanf("%d,%d",&p1->num,&p1->sec);
m++;
}
p1->next=head;
prin(head);
printf("\n");
result(head);
}



/实现主要功能/(可能这里错了)
struct data * result(struct data *head1)
{struct data *p1,*p2;int i,a;
p2=p1=head1;
printf("the number you want to circle firstly:\n");
scanf ("%d",&a);
printf("the result is:\n");
while(p2!=p2->next&&p1!=p1->next)
{ {for(i=1;i<=a;i++)
p2=p1;
p1=p2->next;
}
printf("%d,%d\n",p1->num,p1->sec);
a=p1->sec;
p2->next=p1->next; p1=p1->next;
}
if(p1==p1->next) printf("%d,%d",p1->num,p1->sec);
else printf("%d,%d",p2->num,p2->sec);

}


/输出链表/
struct data *prin(struct data *head2)
{struct data *p1,*p2;
p1=head2;
printf("%d,%d ",p1->num,p1->sec);
p1=head2->next;
while(p1!=head2)
{printf("%d,%d ",p1->num,p1->sec);
p1=p1->next;
}
}




...全文
134 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenhu_doc 2006-04-19
  • 打赏
  • 举报
回复
//前不久为伊写了一个,贴出来

#include <iostream>
#include <iomanip>
using namespace std;

typedef struct LNode{
int NO;
struct LNode * next;
}Lnode, *Josephus;

Josephus InitJosephus()
{
LNode * head = new LNode;
head->next = head;
head->NO = 0;
return head;
}

Josephus CreateJosephus(int enjoyIn)
{
LNode * node = NULL;
LNode * head = InitJosephus();
LNode * forword = head;

for( int i = 1; i <= enjoyIn; i++ )
{
node = new LNode;
node->NO = i;
forword->next = node;
forword = forword->next;
node->next = NULL;
}
forword->next = head->next;
return head;
}

void JoseProcess( Josephus &Jose, int position, int cycle)
{
Josephus p = Jose->next;
Josephus qTemp = NULL;
int temp;
for(int i = 0; i < position-1; i++ )
{
p = p->next;
}
cout<<"Out of the queue... "<<endl;

if(cycle == 1 && p->next != p)
{
qTemp = p;
while( qTemp->next != p )
qTemp = qTemp->next;
}
if(cycle == 1 && p->next == p)
{
cout<<"The winner is "<< p->NO <<" child! "<<endl;

}
while( p->next != p )
{

for(int j = 1; j < cycle; j++ )
{
qTemp = p;
p = p->next;
}

temp = p->NO;

qTemp->next = p->next;
delete p;
p = qTemp->next;
cout<<temp<<endl;
}
cout<<"The winner is "<< p->NO <<" child! "<<endl;
}

void main()
{
int enjoyIn;
int position;
int cycle;

cout<<"the total of the children : ";
cin>>enjoyIn;
cout<<endl<<"the position you want to access : ";
cin>>position;
cout<<endl<<"the cycle you want : ";
cin>>cycle;
cout<<endl;

Josephus Jose = CreateJosephus( enjoyIn );
JoseProcess( Jose, position, cycle);
}
jixingzhong 2006-04-19
  • 打赏
  • 举报
回复
/*约瑟夫环--链表,楼主自己比较下问题何在~*/

#include <stdlib.h>
#include <stdio.h>
typedef struct node
{
int data;
struct node *next;
}LNode;

main()
{
LNode* Create(int,int);
LNode* GetNode(LNode *);
int Print(LNode *,int);
LNode *p;
int n,k,m;
do
{
printf ("输入总人数");
scanf ("%d",&n);
}
while (n<=0);
do
{
printf ("输入开始人的序号(1~%d)",n);
scanf ("%d",&k);
}
while (k<=0 || k>n);
do
{
printf ("输入间隔数字");
scanf ("%d",&m);
}
while(m<=0);

p=Create(n,k);
Print(p,m);
return 0;
};

LNode* Create(int n,int k)/*创建循环链表*/
{
int start=k-1;
LNode *s,*p,*L=0,*t;
if (start==0) start=n;
while (n!=0)
{
s=(LNode *)malloc(sizeof(LNode));
if (L==0) p=s;
if (n==start) t=s;
s->data=n;
s->next=L;
L=s;
n--;
}
p->next=L;
return t;
}

LNode* GetNode(LNode *p)/*出队函数*/
{
LNode *q;
for (q=p;q->next!=p;q=q->next);
q->next=p->next;
free (p);
return (q);
}

Print(LNode *p,int m)/*输出函数*/
{
int i;
printf ("出队编号:\n");
while (p->next!=p)
{
for (i=1;i<=m;i++)
p=p->next;
printf ("%d ",p->data);
p=GetNode(p);
}
printf("%d\n",p->data);
return 0;
}

69,371

社区成员

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

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