📄 yuesefu.txt
字号:
/*
Name: 约瑟夫环
Author:wujilin
Description:
Date: 11-09-06 20:22
Copyright:
*/
#include"stdio.h"
#include"stdlib.h"
#define MAXPASSWORDVALUE 20
#define MAXPERSONNUMBER 30
#define MAXFIRSTCOUNTVALUE 10
#define MAXPASSWORD 10
typedef struct Node
{
int data;
int password;
struct Node *next;
}Node, *LinkList;
////////////////////////////////////////////////
/////函数的声明
////////////////////////////////////////////////
void CreatLinkList(LinkList *);
void InitLinkList(LinkList *,int );
int GetPassword();
int GetPersonNumber();
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("memory allocation failed, goodbye");
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("memory allocation failed, goodbye");
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 > MAXPASSWORDVALUE || password < 0)
{
printf("您输入的数字无效,请输入在0到%d的整数:",MAXPASSWORD);
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 > MAXFIRSTCOUNTVALUE || firstCountValue < 0)
{
printf("\n你输入的数字无效,请输入在0到%d的整数",MAXFIRSTCOUNTVALUE);
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -