求高手帮忙改一个C语言的josephus程序

来源:百度知道 编辑:UC知道 时间:2024/09/19 16:41:28
设n个人,围坐在一个圆桌周围,现在从第s个人开始报数,数到第m个人,让他出局;然后从出局的下一个人重新开始报数,数到第m个人,再让他出局,……,如此反复直到所有的人全部出局为止。

下面要解决的Josephus问题是:对于任意给定的n, s和m,求出这n个人的出局序列。 使用n = 9, s = 1, m = 5,以及n = 9, s = 1, m = 0,或者n = 9, s = 1, m = 10作为输入数据,检查你的程序的正确性和健壮性。最后分析所完成算法的时间复杂度。 使用链表或数组完成。
我的程序,运行时总是有问题,求各位帮忙改一改,好的一定多加分。
#include <stdio.h>
#include<stdlib.h>
struct node
{
int v;
struct node *next;
}*p,*head,*q;

int main()
{
int n,m,s;
int i,j;
printf("input the numerical value of m\n");
scanf("%d",&m);
printf("input the numerical value of n\n");
scanf("%d",&n);
printf("input the numerical value of s\n");
scanf("%d",&s);
head=(struct node*)malloc(sizeof(struct node));
q=head;
for(i=1;i<=n;i++)
{
p->v=i;
p=(struct node*)malloc(sizeof(struct node));

供参考。

#include <iostream>

using namespace std;

int main() {
int m, n;
while (cin >> n >> m) {
int* a = new int[n];
for (int i = 0; i < n; i++) a[i] = i + 1;
int j = 1; //用于报数
int k = 0; //遍历数组
int l = n; //跟踪剩余人数
while (l > 1) {
if (a[k]) {
if (j++ == m) { //报到m出局
j = 1;
a[k] = 0;
l--;
}
}
if (++k == n) k = 0; //循环数组
}
while (a[--i] == 0);
cout << a[i] << endl;
delete[] a;
}
return 0;
}

参考一下这个顺序表
#include<stdio.h>
#include<stdlib.h>
#define max 100
typedef struct node
{
int length;
int data[max];
}LIST,*PLIST;

PLIST init()
{
PLIST list;
list=(PLIST)malloc(sizeof(list));
if(list)
list->length=0;
return list;
}

void insert(PLI