电子工程师技术服务社区
公告
登录
|
注册
首页
技术问答
厂商活动
正点原子
板卡试用
资源库
下载
文章
社区首页
问答
约瑟夫环问题
已解决
73482
个问题
已帮助
5993
位优秀工程师
约瑟夫环问题
大少爷安安
2020-09-10
浏览量:545
#include<stdio.h>
#include<stdlib.h>
#define MAXPV 20 /@@*每人最大密码值为20*/
#define MAXNUM 30 /@@*需要处理的最多人数为30*/
#define MAXFV 10 /@@*初始查找上限值为10*/
typedef struct li
nkList
{
int data;
int password;
struct li
nkList *next;
}li
nkList;
/@@*函数声明*/
li
nkList *CreatList();
void InitList(li
nkList *int );
int GetPassword();
int GetPersonNumber();
int GetFirstCountValue();
void GetOutputOrder(li
nkList* int int int* );
void printResult(int * int );
li
nkList *CreatList()
{ /@@*初始化单链表函数*/
li
nkList *L;
L=(li
nkList *)malloc(sizeof(li
nkList));
if (L==NULL)
{
printf("分配内存失败!");
exit(1);
}
return L;
}
void InitList(li
nkList *L int personNumber)
{ /@@*建立循环单链表函数*/
li
nkList *p*q;
int i;
p=L;
p->data=1;
p->password=GetPassword();
for (i=2; i<=personNumber; i++)
{
q=(li
nkList *)malloc(sizeof(li
nkList));
if(q==NULL)
{
printf("分配内存空间失败!");
exit(1);
}
q->password=GetPassword();
q->data=i;
p->next=q;
p=q;
}
p->next=L;
}
int GetPersonNumber()
{ /@@*输入处理的人数函数*/
int personNumber;
printf("请输入人数:");
scanf("%d"&personNumber);
while(personNumber>MAXNUM || personNumber<0)
{
printf("\n你输入的数字无效,请输入在0到%d的整数"MAXNUM);
scanf("%d"&personNumber);
}
printf("本次求约瑟夫环的出列顺序人数为%d人。\n"personNumber);
return personNumber;
}
int GetPassword()
{ /@@*给每个人赋密码函数*/
int password;
static int count=1;
printf("请输入第%d人的密码:"count);
scanf("%d"&password);
while (password > MAXPV || password<0);
{
printf("您输入的数字无效,请输入在0到%d的整数:"MAXPV);
scanf("%d"&password);
}
count++;
return password;
}
int GetFirstValue()
{ /@@*确定开始的上限值函数*/
int firstValue;
printf("请输入密码的上限值:");
scanf("%d"&firstValue);
while (firstValue>MAXFV || firstValue<0)
{
printf("\n你输入的密码无效,请输入在0到%d的整数:"MAXFV);
scanf("%d"&firstValue);
}
printf("最终的密码上限值为%d。\n"firstValue);
return firstValue;
}
void GetOutputOrder(li
nkList *L int personNumber int reportValue int array[MAXNUM])
{ /@@*得到出列顺序函数*/
li
nkList *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 personNumber)
{ /@@*输出结果函数*/
int i;
printf("\n按每人持有的编号依次出列的顺序为:");
for(i=0; i<personNumber; i++)
printf("%-3d"array[i]);
printf("\n");
}
main()
{
li
nkList *L;
int personNumber reportValue;
int array[MAXNUM];
printf("约瑟夫环问题。\n");
personNumber=GetPersonNumber();
reportValue=GetFirstValue();
L=CreatList();
InitList(L personNumber);
GetOutputOrder(L personNumber reportValue array);
printResult(array personNumber);
system("pause");
return 0;
}
不知道那错了 最后的出列顺序也没问题,为什么说输入的数字无效 求指点
显示全部
其他
关注问题
写回答
0
0
收起
我来回答
上传资料:
选择文件
文件大小不超过15M(格式支持:doc、ppt、xls、pdf、zip、rar、txt)
所有亮答
数量:
0
相关问题
问题达人
换一批
文章
知识经验换现金
换一批
约瑟夫环问题
写回答
关注问题
×
我要举报该内容,理由是:
内容质量差:
内容太水、伸手党
垃圾广告信息:
广告、招聘、推广、测试内容等
偏离问答主题:
与技术无关、讨论类
与社区已有内容重复:
违规内容:
色情、暴力、血腥、敏感信息等
不友善内容:
人事攻击、挑衅辱骂、恶意行为
以上选项都不是: