标题:约瑟夫问题,运行出现了乱码
只看楼主
jianfeng
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2006-11-20
 问题点数:0 回复次数:1 
约瑟夫问题,运行出现了乱码

#include"stdio.h"
#include"stdlib.h"
#define MAXPERSONNUMBER 100
typedef struct Node
{
int data;
int password;
struct Node *next;
}Node, *LinkList;
void CreatLinkList(LinkList *);
void InitLinkList(LinkList *,int );
int personnumber();
int GetPassword();
int password();
int GetPersonNumber();
int GetFirstCountValue();
void GetOutputOrder(LinkList* , int, int, int* );
void printResult(int * ,int );
void CreatLinkList(LinkList *L)
{
(*L) = (LinkList)malloc(sizeof(Node));
if ((*L) == NULL)
{
printf("failed ");
exit(1);
}
}
void InitLinkList(LinkList *L, int personNumber)
{
Node *p, *q;
int i ;
p = (*L);
p->data = 1;
p->password = GetPassword();
for (i = 2; i <= personNumber; i++)
{
q = (LinkList)malloc(sizeof(Node));
if (q == NULL)
{
printf("failed");
exit(1);
}
q->password = GetPassword();
q->data = i;
p->next = q;
p = q;
}
p->next = (*L);
}
int GetPassword()
{
int password;
static int count = 1;
printf("\n请输入第%d的密码:",count);
scanf("%d",&password);
while (password < 0)
{
printf("您输入的数字无效,请输入在0到%d的整数:",password);
scanf("%d",&password);
}
printf("第%d个人的密码为%d",count,password);
count++;
return password;
}
int GetPersonNumber()
{
int personNumber;
printf("请输入需要输入人的数目:");
scanf("%d",&personNumber);
while (personNumber > MAXPERSONNUMBER || personNumber < 0)
{
printf("\n你输入的数字无效,请输入在0到%d的整数",MAXPERSONNUMBER);
scanf("%d",&personNumber);
}
printf("最终确定的人数为%d\n",personNumber);

return personNumber;
}
int GetFirstCountValue()
{
int firstCountValue;
printf("请输入初始的上限值");
scanf("%d",&firstCountValue);
while (firstCountValue < 0)
{
printf("\n你输入的数字无效,请输入在0到%d的整数",firstCountValue);
scanf("%d",&firstCountValue);
}
printf("最终的上限值为%d",firstCountValue);
return firstCountValue;
}
void GetOutputOrder(LinkList *L, int personnumber, int reportValue, int array[MAXPERSONNUMBER])
{
Node *p, *q;
int count = 1, i = 0;
p = (*L);
while (personnumber)
{
while (count != reportValue)
{
q = p;
p = p->next;
count++;
}

array[i++] = p ->data;
reportValue = p->password;
q->next = p->next;
free(p);
p = q->next;
count = 1;
personnumber--;
}
}
void printResult(int array[],int personnumer)
{
int i;
printf("\n出队的顺序为:");
for(i = 0; i < personnumer; i++)
{
printf("%-3d",array[i]);
}
printf("\n");
}
int main(void)
{
LinkList L;
int personnumber, reportValue;
int array[MAXPERSONNUMBER];
personnumber = GetPersonnumber();
reportValue = GetFirstCountValue();

CreatLinkList(&L);
InitLinkList(&L, personnumber);
GetOutputOrder(&L, personnumber, reportValue,array);
printResult(array, personnumber);

system("pause");
return 0;
getchar();getchar();
}

搜索更多相关主题的帖子: int 约瑟夫 void 乱码 LinkList 
2007-03-19 01:14
wyzn12
Rank: 1
等 级:新手上路
帖 子:129
专家分:0
注 册:2006-10-28
得分:0 

以前编过,你参考参考:

一.数组

#define N 8
#define M 3
main()
{
int a[N],i,flag=0,num_flag=0;
for(i=0;i<N;i++)
a[i]=i+1;
printf("\noutput:\n");
i=0;
while(num_flag<N)
{
if(a[i]!=0)
{
flag++;
if(flag%M==0)
{
printf("=>%d",a[i]);
num_flag++;
a[i]=0;
}
}
i++;
if(i==N)i=0;
}
getch();
}

二.环链表

#include<malloc.h>

#define LEN sizeof(struct person)
struct person
{
int num;
struct person *next;
};
struct person *creat(int n)
{
struct person *head,*p1,*p2;
int i;
p1=p2=(struct person *)malloc(LEN);
head=NULL;
for(i=1;i<=n;i++)
{
if(i==1)
{head=p1;head->num=1;}
else
p2->next=p1;
p2=p1;
p1=(struct person *)malloc(LEN);
p1->num=i+1;
}
p2->next=head;
return(head);
}

main()
{
struct person *head,*p,*p2;
int M,N;
int i,j;
i=1;
j=1;
printf("\ninput M=:");
scanf("%d",&M);
printf("input N=:");
scanf("%d",&N);
head=creat(M);
p=head;
printf("\noutput:\n");
while(p&&j<=M)
{
if(N==1)
{j=j+1;
printf("=>%d",p->num);
p=p->next;
}
else
{
p2=p;
p=p->next;
i=i+1;
if(i%N==0)
{
p2->next=p->next;
printf("=>%d",p->num);
p->next=NULL;
free(p);
p=p2->next;
i=i+1;
j=j+1;
}
}
}
getch();
}


新王登基,血流成河!
2007-03-19 12:10



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-125062-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.462820 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved