算法-约瑟夫环问题

1.问题:

请注意:这一节部分只有count_off函数部分可以修改,要先理解其他部分在做什么后再下手实现count_off函数喔

在学习了链表结构后,这一节我们需要用链表解决一个稍有改动的“约瑟夫环(Josephus problem)”问题:

计算理工学院有 N 个同学,围成了一个圆圈,每人被顺序地编了一个序号(分别为 1,2,3... n1,2,3...n),从编号为 K 的人开始报 1,他之后(顺初始数字增长方向计算序号)的人报 2,以此类推,数到某一个数字 M 的人出列。出列同学的下一个人又从 1 开始继续报数,数到某一个数字 M 的人出列。不断重复这一过程,直到所有人都出列为止。

你需要根据同学人数 N 和给出的 K 和 M 计算出同学的正确出列顺序。

这一题的main函数已经帮你写好了,同时,已经帮你定义了一个节点的结构体类型、通过circle_create创建了一个循环链表。

现在请在count_off函数中根据传入的编号为 1 的节点 headhead、学生数 n、起始报数学生编号 k、数到出列的数字 m 实现报数的过程,按照题目要求进行输出。

输入格式

测评机会反复运行你的程序。每次程序运行时,输入为一行,包括三个被空格分隔开的符合描述的正整数 N、K 和 M(1<= K<= N<= 10001≤K≤N≤1000,1 <= M <= 20001≤M≤2000)。

输出格式

输出为一行,包含 N 个整数,为依次顺序出列的学生编号,由空格分隔开。

样例输入1

9 1 1

样例输出1

1 2 3 4 5 6 7 8 9

样例输入2

8 5 2

样例输出2

6 8 2 4 7 3 1 5

知识点:

1.指针

2.链表

3.动态内存分配 ,动态内存释放

重点:

    1.需要考虑到头尾相接的问题

    2.需要得到当前人的上一个人的指针(非常重要,我就是没注意到这一点,卡了半小时找问题)

具体代码如下:

#include <stdio.h>
#include <stdlib.h>
typedef struct node {
    int data;
    struct node *next;
} Node;

Node *circle_create(int n);
void count_off(Node *head, int n, int k, int m);

int main() {
    int n, k, m;
    scanf("%d%d%d", &n, &k, &m);
    Node *head = circle_create(n);
    count_off(head, n, k, m);
    return 0;
}

Node *circle_create(int n) {
    Node *temp, *new_node, *head;
    int i;

    // 创建第一个链表节点并加数据
    temp = (Node *) malloc(sizeof(Node));
    head = temp;
    head->data = 1;

    // 创建第 2 到第 n 个链表节点并加数据
    for(i = 2; i <= n; i++) {
        new_node = (Node *) malloc(sizeof(Node));
        new_node->data = i;
        temp->next = new_node;
        temp = new_node;
    }

    // 最后一个节点指向头部构成循环链表
    temp->next = head;

    return head;
}

void count_off(Node *head, int n, int k, int m) {
    Node *tempk = head;
    Node *lasttemp = head;
    if(tempk->data == k){
        //如果是头部,则循环到最后一个
        for(int j=0;j<n;j++){
            if(j == n-1){
                break;
            }
            lasttemp = lasttemp->next; 
        }
    }else{
        for(int i=0;i<n;i++){
            if(tempk->data == k-1){
                lasttemp = tempk;
                tempk = tempk->next;
                break;
            }
            tempk = tempk->next;
        }
    }
    Node *temp = tempk;
    for(int i = 0;i<n;i++){
        for(int j = 0;j<m;j++){
            if(j == m-1){
                printf("%d",temp->data);
                if(i!=n-1) printf(" ");
                if(temp == head){
                    tempk = head;
                    lasttemp->next = head->next;
                    head = head->next;
                    temp = head;
                }   
                else{
                    tempk=temp;
                    lasttemp->next = temp->next;
                    temp=temp->next;   
                }
                free(tempk);
                tempk = NULL;    
                break;
            }else{
                lasttemp = temp;   
                temp = temp->next;
            }
        }
    }
    return;
}

猜你喜欢

转载自blog.csdn.net/qq_34970171/article/details/115597216