Menu
CoddyTech

练习 #4

Coddy 队列:数据结构系列 #2 课程的第 12 课,共 12 课。

接下来的挑战需要使用队列。已经为你提供了 Queue 数据结构,快来使用吧!

challenge icon

挑战

简单

编写一个名为 hotPotato的函数,它接收一个姓名列表和传递次数,并模拟击鼓传花游戏:传递花球tosses次(每次传递都会将队首的人移到队尾),然后淘汰此时位于队首的人。重复此过程,直到只剩下一个人,并返回这个人的姓名。

使用提供的 Queue 来解决这个问题!

亲手试试

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "solution.h"

int main() {
    char line[4096];
    char** names = (char**)malloc(sizeof(char*) * 1000);
    int n = 0;
    if (fgets(line, sizeof(line), stdin)) {
        char* tok = strtok(line, " \t\r\n");
        while (tok) { names[n++] = strdup(tok); tok = strtok(NULL, " \t\r\n"); }
    }
    int tosses = 0;
    scanf("%d", &tosses);
    printf("%s\n", hotPotato(names, n, tosses));
    return 0;
}

队列:数据结构系列 #2中的所有课

自己练习: C 在线编译器