哪位同学最优秀?

有一天,结束了一天面试后,boss 跑来问蒜头君:“小蒜,你觉得今天面试的同学里面,谁最优秀呀,谁最适合写课程呀?”蒜头君递给 boss 一沓简历,回答到:“这里有 N 份简历,boss 你猜猜哪位同学最优秀。”

每份简历都有一个对应的 id,编号从 1 开始,依次从第一份简历到最后一份简历。boss 会从简历里抽掉 M 份简历,每次他随机念一个数字 numi​,然后从第一份简历开始数,数到第 numi 份时,就会把对应的简历抽掉,接着念下一个数字。抽掉 M 份简历后,boss 从剩余的简历中,取出最中间的一份简历,然后点点头念道:“我相信这位同学一定最优秀,哈哈”。

现在蒜头君想知道这份简历的 id 是多少,聪明的你能帮他算出来吗?
输入格式

第一行输入两个正整数 N 和 M(1≤M<N≤1000)。第二行输入 M 个整数 numi(1≤numi≤1000),表示 boss 依次念出来的数字。

保证 N−M 是奇数,输入的 numi 小于等于当前剩余简历数量。
输出格式

输出为一行,输出 boss 认为最优秀的同学的 id 是多少。

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

LinkedList insert(LinkedList head, Node *node, int index ){
   if (head == NULL) {
       if (index != 0){
          //printf("插入失败\n");
           return head;
       }
       head = node;
       //printf("插入成功\n");
       return head;
   }
    if (index == 0) {
        node->next = head;
        head = node;
        return head;
    }
    Node *current_node = head;
    int count = 0;
    while (current_node->next != NULL && count < index - 1) {
        current_node = current_node->next;
        count++;
    }
    if (count == index - 1 ) {
        node->next = current_node->next;
        current_node->next = node;
    }
    return head;
}

LinkedList delete_node(LinkedList head, int index){
    if (head == NULL) {
        return head;
    }
    Node *current_node = head;
    int count = 0;
    if (index == 0) {
        head = current_node->next;
        free(current_node);
        return head;
    }
    for (int i = 0; i < index - 1; i++) {
        current_node = current_node->next;
        count++;
    }
    if (count == index - 1 ) {
       Node *delete_node = current_node->next;
       current_node->next = delete_node->next;
        free(delete_node);
    } 
    return head;
}

void out_put(LinkedList head) {
    if (head == NULL) {
        return ;
    }
    Node *current_node = head;
    while(current_node != NULL) {
        printf("%d ",current_node->data);
        current_node = current_node->next;
    }
    printf("\n");
}

void print_id(LinkedList head,int n) {
    Node *current_node = head;
    for (int i = 0; i < n/2; i++) {
        current_node = current_node->next;
    }
    printf("%d",current_node->data);
}
void main(){
    int m,n;
    int num[1000];
    scanf("%d%d",&n,&m);
    LinkedList linkedlist = NULL;
    for (int i = 0; i < n; i++) {
        Node *node = (Node *)malloc(sizeof(Node));
        node->data = i + 1;
        node->next = NULL;
        linkedlist = insert(linkedlist,node,i);
        //out_put(linkedlist);
    }
    
    for (int j = 0; j < m; j++) {
        scanf("%d",&num[j]);
        //printf("num[j]=%d\n",num[j]);
        linkedlist = delete_node(linkedlist,num[j] - 1);
        n--;
        
    }
    //out_put(linkedlist);
    print_id(linkedlist,n);
    return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yitahutu79

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值