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

#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



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




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

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