约瑟夫问题

来源:互联网 发布:编程需要哪些英语单词 编辑:程序博客网 时间:2024/06/05 14:32

用顺序表实现,也可用链表实现。

#include <stdio.h>#include <stdlib.h>#define MAX_SIZE 1024typedef struct node{int data[MAX_SIZE];int length;}SeqList,*PSeqList;/************************************************************顺序表初始化*************************************************************/PSeqList Init_SeqList(void){PSeqList PL;PL = (PSeqList)malloc(sizeof(SeqList));if (PL){for (int i = 0; i < MAX_SIZE; i++)PL->data[i] = 0;PL->length = 0;}return (PL);}/************************************************************输出整个顺序表*************************************************************/int DisplayAll_SeqList(PSeqList pl){if (!pl){printf("SeqList Not Exist\n");return(0);}if (pl->length<=0){printf("SeqList Empty\n");return(0);}for (int i = 0; i < pl->length;i++)printf("%d\n", pl->data[i]);return(1);}/************************************************************输出顺序表某个位置*************************************************************/int DisplaySingle_SeqList(PSeqList pl,int n){if (!pl){printf("SeqList Not Exist\n");return(0);}if (pl->length <= 0){printf("SeqList Empty\n");return(0);}printf("%d\n", pl->data[n-1]);return(1);}/************************************************************顺序表求长度*************************************************************/int Length_SeqList(SeqList pl){return (pl.length);}/************************************************************顺序表检索*************************************************************/int Location_SeqList(PSeqList L, int x){int i = 0;while (i < L->length && L->data[i] != x)i++;if (i >= L->length){printf("Searching Failure\n");return(0);}elsereturn(i + 1);}/************************************************************顺序表插入*************************************************************/int Insert_SeqList(PSeqList L, int location, int data){if (!L){printf("SeqList Not Exist\n");return(0);}if (L->length >= MAX_SIZE){printf("SeqList Full");return(0);}if (location < 1 || location > L->length + 1)//保证在顺序表空时可插入,note that 插入位置是 1<=location<=length+1 !!!{printf("Location Illegal\n");return(0);}for (int i = L->length - 1; i >= location - 1; i--)//当是在length+1位置插入或是在SeqList为空时插入不执行该循环L->data[i + 1] = L->data[i];L->data[location - 1] = data;L->length++;printf("Insert Success\n");return(1);}/************************************************************顺序表删除*************************************************************/int Delete_SeqList(PSeqList pl, int location){if (!pl){printf("SeqList Not Exist\n");return(0);}if (pl->length <= 0){printf("SeqList Empty\n");return(0);}if (location < 1 || location > pl->length){printf("Location Illegal\n");return(0);}for (int i = location; i < pl->length; i++)pl->data[i - 1] = pl->data[i];pl->length--;printf("Delete Success\n");return(1);}/************************************************************求交集*************************************************************/void Inter_sec(PSeqList pa, PSeqList pb){for (int i = 0; i < pa->length; i++){if (!Location_SeqList(pb, pa->data[i]))Delete_SeqList(pa, i + 1);}}/************************************************************求并集*************************************************************/void Merge_sec(PSeqList pa, PSeqList pb){for (int i = 0; i < pb->length; i++){if (!Location_SeqList(pa, pb->data[i]))Insert_SeqList(pa,pa->length+1,pb->data[i]);}}/************************************************************约瑟夫问题*************************************************************/int josephus_problem(PSeqList js, int s, int m){int i = 0, w = 0;if (!js->length){printf("SeqList Empty");return(0);}i = s - 1;while (js->length){i = (i + m - 1) % js->length;w = js->data[i];printf("%d\n", w);Delete_SeqList(js, i + 1);}return(1);}int main(){/*Construct a SeqList*/PSeqList pl = Init_SeqList();for (int i = 0; i < 1000;i++)pl->data[i] = i+1;pl->length = 1000;/*Solving*/josephus_problem(pl,1,7);while (true){}return(1);}


0 0
原创粉丝点击