您的位置:首页 > 其它

约瑟夫环问题

2011-07-30 23:28 387 查看
#include <stdafx.h>

#include <stdlib.h>

struct number

{

int num;

struct number * next;

};

void main ()

{

int m, n;

struct number * p, * head=NULL, * tail;

printf("please input M and N:\n");

scanf("%d %d", &m, &n); //输入M、N值。

for (int i=1; i<=n; i++) //建立循环链表。

{

p=(struct number *)malloc(sizeof(struct number));

p->num=i;

if(head==NULL){

head=p;

tail=p;//注意开始tail也要赋值

}

else

tail->next=p;

tail=p;

}

tail->next=head;

p = tail; //从head开始,记录开始的前一个指针

while(n--) //剩下的数的个数为n

{ int t = m%n; //防止多数太多圈造成时间浪费

for(int j=1; j<t;j++ ) //数到要删的那个数的前一个

p=p->next;

number *q = p->next; //要删的数的指针

printf("%d ", q->num); //输出要删的数

p->next = q->next; //要删的数从链表中去掉

free(q);

}

printf("\n");

}

转载:http://apps.hi.baidu.com/share/detail/13574526

用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出 zt

题目是这样:用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至最后一个元素并输出该元素的值。写出C程序。

其实现代码如下(在READHAT9.0 LINUX下测试通过):

/********************************************

*文件名:game_ouputnum.h

*文件描述:给定M,N值,从1至N开始顺序循环数数,每数到M,把该M去掉,继续直到最后一个元数并输出该无数

使用循环链表实现.

*作 者:szxf

*版本号:1.0.0

*版 权:

*修改记录:

*********************************************/

#include <stdio.h>

#include <stdlib.h>

/* N 值 */

#define NUM 8   

#define OK 1       

#define ERROR -1

/* M 值 */ 

#define NEXT 3      

typedef struct

{

int data;

struct list *next;

}list;

/******************************************************

*函数名:list *createList()

*参 数:无

*功能描述:创建一个循环链表

*返回值:成功返回链表的首指针,失败返回-1

*抛出异常:

*作 者:szxf

******************************************************/

list *createList()

{

list *head=NULL;

list *tail=NULL,*temp=NULL;

int i=0;

head=(struct list *)malloc(sizeof(list));

if( head == NULL )

{

printf("malloc space failed!\n");

return ERROR;

}

head->data=1;

head->next=head;

tail = head;

for( i=2;i<=NUM; i++)

{

temp=(struct list *)malloc(sizeof(list));

if( temp == NULL )

{

printf("malloc space failed!\n");

return ERROR;

}

temp->data=i;

temp->next=head;

tail->next = temp;

tail = temp;

}

return head;

}

/******************************************************

*函数名:void printList(list *head)

*参 数:链表的首地址

*功能描述:打印出链表中所有的数据

*返回值:无

*抛出异常:无

*作 者:szxf

******************************************************/

void printList(list *head)

{

list *pList=NULL;

pList = head;

if( head == NULL)

{

printf( "printList param invalid!\n" );

}

while(head->next!=pList)

{

printf("this data is :%d\n", head->data);

head=head->next;

}

printf("the data is :%d\n",head->data);

return;

}

/******************************************************

*函数名:int getLastElem(list *pList)

*参 数:链表的首地址

*功能描述:开始顺序循环数数,得到最后一个元素的值.

*返回值:正确返回最后一个元素的值,错误返回-1

*抛出异常:无

*作 者:无

******************************************************/

int getLastElem(list *pList)

{

list *head=NULL,*temp=NULL;

int i = 1;

if(pList == NULL)

{

printf("getLastElem param invalid\n");

return ERROR;

}

head=temp=pList;

while(head!=head->next)

{

if(i==NEXT)

{

temp=head;

head=head->next;

free(temp);

pList->next=head;

temp=NULL;

i=1;

}

else

{

pList=head;

head=head->next;

i++;

}

}

printf("get lastElem:%d\n", head->data);

return head->data;

}

int main()

{

list *pList=NULL;

pList=createList();

if(pList==NULL)

{

printf("create list error!\n");

exit(0);

}

printList(pList);

printf("the last elem:%d\n",getLastElem(pList));

}

转载:/article/10118337.html

约瑟夫环:用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至最后一个元素并输出该元素的值

约瑟夫环主要是要定义一个循环链表,自己写的,经过验证。

typedef struct node
{
int num;
struct node *next;
}Node; //定义一个链表结构体
int main()
{
int t =0;
int n=20,m=3;
Node *head=NULL;
Node *wei = NULL;
//malloc后需要free掉,否则当程序
//长时间运行时循环malloc的情况下会崩溃,

//内存泄露,程序关闭时
//操作系统会自动释放掉开辟内存,这里测试就不释放

head = (Node *)malloc(sizeof(Node));
head->num=1;
head->next = NULL;
wei = head;
for(int i=2;i<=n;i++)
{
Node *temp = (Node *)malloc(sizeof(Node));
temp->num = i;
temp->next = NULL;
wei->next = temp;
wei = temp;
}
wei->next= head; //定义一个循环链表

//输出合适的值
Node *head1= wei;
while(t<20)
{
int x= 0;
while(x!=m)
{
head1=head1->next;
x++;
}
printf(" %d",(head1)->num);
t++;
}
getchar();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: