C语言双向约瑟夫问题,输出那边不清楚哪里出错了,free也有问题,求各位大神帮忙看一下程序TUT

nihuayewoa 2017-10-18 12:09:24
约瑟夫问题是一个经典的问题,我们不妨将这个经典问题进行扩展,变成一个双向的约瑟夫问题。

  已知n个人(不妨分别以编号1,2,3,…,n 代表 )围坐在一张圆桌周围,首先从编号为 k 的人从1开始顺时针报数,1, 2, 3, ...,记下顺时针数到 m 的那个人,同时从编号为 k 的人开始逆时针报数,1, 2, 3, ...,数到 m 后,两个人同时出列。然后从出列的下一个人又从 1 开始继续进行双向报数,数到m的那两个人同时出列,…;。依此重复下去,直到圆桌周围的人全部出列。直到圆桌周围只剩一个人为止。

  如果双向报数报到 m 时落在同一个人身上,那本次出列的只有一个人。

  例如:5,1,2。则总共5个人,从1开始顺时针报数,数到2,定位编号2;同时从1开始报数数到2,定位编号5;2和5同时出列。然后继续开始报数,顺时针报数3,4,定位到4;逆时针报数4,3,定位3;4和3同时出列。最后剩余的为编号1。输出为:2-5,4-3,1,。

  如果输入:6,2,3。则输出:4-6,2,1-3,5,。其中第2次只输出一个2,表示第二次双向报数时,恰好都落在编号2上,所以只有一个编号出列。

输入
n,k,m

输出
按照出列的顺序依次输出编号。同时出列编号中间用减号“-”连接。

非法输入的对应输出如下
a)

输入:n、k、m任一个为0
输出:n,m,k must bigger than 0.

b)

输入:k>n
输出:k should not bigger than n.

测试用例1:6,2,3 结果:4-6,2,1-3,5,
测试用例2:5,1,2 结果:2-5,4-3,1,

程序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct seat{
int num;
struct seat * prior;
struct seat * next;
};

int main()
{
int k,n,m,i,count=1,j,numl=0,numr=0;
scanf ("%d,%d,%d",&n,&k,&m);
// k是初始座位,m是数的个数
struct seat * head ,* p,* q,* tail,* r,*s;
head = (struct seat * ) malloc (sizeof(struct seat));
tail = (struct seat * ) malloc (sizeof(struct seat));
head->num = -1;
head->next = head;
head->prior = head;

if(k<1||n<1||m<1)
printf("n,m,k must bigger than 0.\n");
else if (k>n)
printf("k should not bigger than n.\n");
else
{
for (i=n;i>0;i--)
{
p = (struct seat *)malloc(sizeof(struct seat));
p->num = i;
p->prior = head;
p->next = head->next;
head->next = p;
p->next->prior = p;
}

while (p->next != head)
p = p->next;
p->next = head->next;
p->next->prior = p;

while (p->num != k)
p = p->next;
r = p;

while (n)
{
for (j=1;j<m;j++)
p = p->next;
q = p;
numr = q->num;

for (j=1;j<m;j++)
r = r->prior;
s = r;
numl = s->num;

if (numr==numl)
{
printf("%d",numr);
n--;
q->prior->next = q->next;
q->next->prior = q->prior;
free(q);
}
else
{
printf("%d-%d",numr,numl);
n=n-2;
q->prior->next = q->next;
q->next->prior = q->prior;
free(q);
s->prior->next = s->next;
s->next->prior = s->prior;
free(s);
}

if (n != 0) printf(",");
}
printf("\n");
}
return 0;
}
...全文
209 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
nihuayewoa 2017-10-20
  • 打赏
  • 举报
回复
引用 1 楼 cfjtaishan 的回复:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct seat{
    int num;
    struct seat * prior;
    struct seat * next;
};

void create_link(struct seat **pphead, int n)
{
    int i;
    struct seat *p, *pcur;

    if (!(*pphead)) {
        *pphead = (struct seat * ) malloc (sizeof(struct seat));
        if (!(*pphead)) {
            fprintf(stderr, "malloc error!\n");
            exit(0);
        }
    }
    (*pphead)->num = n;
    (*pphead)->next = *pphead;
    (*pphead)->prior = *pphead;

    pcur = *pphead;
    for (i = n-1; i > 0; i--) {
        p = (struct seat *)malloc(sizeof(struct seat));
        if (!p) {
            fprintf(stderr, "malloc error!\n");
            exit(0);
        }
        p->num = i;
        p->prior = pcur;
        p->next = pcur->next;
        pcur->next = p;
        pcur = p;
    }
}

void show_list(struct seat *phead)
{
    struct seat *pcur = phead;

    printf("Positive: ");
    while (pcur->next != phead) {
        printf("%d\t", pcur->num);
        pcur = pcur->next;
    }
    printf("%d\n", pcur->num);

    printf("Reverse: ");
    while (pcur != phead) {
        printf("%d\t", pcur->num);
        pcur = pcur->prior;
    }
    printf("%d\n", pcur->num);
}

struct seat *delete_link(struct seat *phead, int start, int num)
{
    struct seat *pcur = phead, *pdel;
    int i = 0;

    while (i++ < start)
        pcur = pcur->next;

    i = 0;
    while (pcur->next != pcur) {
        if ((i+1) % num == 0) {
            pdel = pcur;
            pcur->prior->next = pcur->next;
            pcur->next->prior = pcur->prior;
            printf("%d is out!\n", pdel->num);
            pcur = pcur->next;
            i++;
            free(pdel);
        } else {
            i++;
            pcur = pcur->next;
        }
    }

    return pcur;
}

int main(void)
{
    int n, start, m;
    struct seat *phead = NULL, *last = NULL;

    printf("Please input n, start  and m(n,start,m): ");
    scanf("%d,%d,%d", &n, &start, &m);

    if (start > n) {
        fprintf(stdout, "num shold be bigger than start!\n");
        return 0;
    }
    create_link(&phead, n);
    show_list(phead);

    last = delete_link(phead, start, m);
    printf("Last node: %d\n", last->num);
    free(last);
    phead = NULL;

    return 0;
}
参考一下吧~ 代码都写到main函数里不是一种好的风格,建议根据功能划分,运用不同的子函数去实现每个划分的功能,然后在main函数里调用。 对于这个需求,建议采用无哨兵的双向循环链表
好的谢谢~
自信男孩 2017-10-18
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct seat{
    int num;
    struct seat * prior;
    struct seat * next;
};

void create_link(struct seat **pphead, int n)
{
    int i;
    struct seat *p, *pcur;

    if (!(*pphead)) {
        *pphead = (struct seat * ) malloc (sizeof(struct seat));
        if (!(*pphead)) {
            fprintf(stderr, "malloc error!\n");
            exit(0);
        }
    }
    (*pphead)->num = n;
    (*pphead)->next = *pphead;
    (*pphead)->prior = *pphead;

    pcur = *pphead;
    for (i = n-1; i > 0; i--) {
        p = (struct seat *)malloc(sizeof(struct seat));
        if (!p) {
            fprintf(stderr, "malloc error!\n");
            exit(0);
        }
        p->num = i;
        p->prior = pcur;
        p->next = pcur->next;
        pcur->next = p;
        pcur = p;
    }
}

void show_list(struct seat *phead)
{
    struct seat *pcur = phead;

    printf("Positive: ");
    while (pcur->next != phead) {
        printf("%d\t", pcur->num);
        pcur = pcur->next;
    }
    printf("%d\n", pcur->num);

    printf("Reverse: ");
    while (pcur != phead) {
        printf("%d\t", pcur->num);
        pcur = pcur->prior;
    }
    printf("%d\n", pcur->num);
}

struct seat *delete_link(struct seat *phead, int start, int num)
{
    struct seat *pcur = phead, *pdel;
    int i = 0;

    while (i++ < start)
        pcur = pcur->next;

    i = 0;
    while (pcur->next != pcur) {
        if ((i+1) % num == 0) {
            pdel = pcur;
            pcur->prior->next = pcur->next;
            pcur->next->prior = pcur->prior;
            printf("%d is out!\n", pdel->num);
            pcur = pcur->next;
            i++;
            free(pdel);
        } else {
            i++;
            pcur = pcur->next;
        }
    }

    return pcur;
}

int main(void)
{
    int n, start, m;
    struct seat *phead = NULL, *last = NULL;

    printf("Please input n, start  and m(n,start,m): ");
    scanf("%d,%d,%d", &n, &start, &m);

    if (start > n) {
        fprintf(stdout, "num shold be bigger than start!\n");
        return 0;
    }
    create_link(&phead, n);
    show_list(phead);

    last = delete_link(phead, start, m);
    printf("Last node: %d\n", last->num);
    free(last);
    phead = NULL;

    return 0;
}
参考一下吧~ 代码都写到main函数里不是一种好的风格,建议根据功能划分,运用不同的子函数去实现每个划分的功能,然后在main函数里调用。 对于这个需求,建议采用无哨兵的双向循环链表

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧