哪位同学最优秀

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

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

输入格式

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

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

输出格式

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

样例输入

7 2
2 4
样例输出

4
提示信息

开始序列是:1 2 3 4 5 6 7, 抽掉第 2 份简历后的序列是:1 3 4 5 6 7, 抽掉第 4 份简历后的序列是:1 3 4 6 7。 最后简历里最中间的是 id 为 4 的简历。 请用单链表解决这道难题。

编译超时—————————————————————————————

using namespace std;

#include <iostream>



class Node{
public :
    int data;
    Node *next;
    Node(int _data){
        data=_data;
        next=NULL;
    }
};

class LinkList{
public:
    Node *head;
    LinkList(){
        head=NULL;
    }
    void insert(Node *node,int index){
        if(index<0){
            return;
        }
        if(head==NULL){
            head=node;
            head->next=NULL;
            return;
        }
        if(index==0){
            node->next=head;
            head=node;
            return;
        }
        Node *current_node=head;
        int count=0;
        while(current_node->next!=NULL&&count<index-1){
            count++;
            current_node=current_node->next;
        }
        if(count==index-1){
            node->next=current_node->next;
            current_node->next=node;
            return;
        }
        return;
    }

    void delete_node(int index) {
        if (head == NULL) {
            return;
        }
        Node *current_node = head;
        int count = 0;
        if (index == 0) {
            head = head->next;
            delete current_node;
            return;
        }
        while (current_node->next != NULL && count < index -1) {
            current_node = current_node->next;
            count++;
        }
        if (count == index - 1 && current_node->next != NULL) {
            Node *delete_node = current_node->next;
            current_node->next = delete_node->next;
            delete delete_node;
        }
    }

    int search(int index){
        if(index<0){
            return -1;
        }
        Node *current_node=head;
        int count=0;
        while(current_node->next!=NULL&&count<index){
            current_node=current_node->next;
            count++;
        }
        if(count==index&&current_node!=NULL){
            return current_node->data;
        }
        return -2;
    }

    void show(){
        Node *current_node=head;
        while(current_node!=NULL){
            //cout<<current_node->data<<" ";
            current_node=current_node->next;
        }
        cout<<endl;
    }

    int getLenth(){
        int len=0;
        Node *current_node=head;
        while(current_node!=NULL){
            len++;
            current_node=current_node->next;
        }
        return len;
    }

};

void main(){
    int N,M;
    cin>>N>>M;
    LinkList *linklist=new LinkList();
    int *key=new int[M];
    for(int i=1;i<=N;i++){
        Node *node=new Node(i);
        linklist->insert(node,i-1);
    }
    //linklist->show();
    for(int j=0;j<M;j++){
        cin>>key[j];
        linklist->delete_node(key[j]-1);
        //linklist->show();
    }
    int count=N-M;
    cout<<linklist->search((count+1)/2-1)<<endl;
}

AC————————————————————————————

using namespace std;
#include <iostream>

class Node{
public :
    int data;
    Node *next;
    Node(int _data){
        data=_data;
        next=NULL;
    }
};

class LinkList{
public:
    Node *head;
    LinkList(){
        head=NULL;
    }
    void insert(Node *node,int index){
        if(index<0){
            return;
        }
        if(head==NULL){
            head=node;
            head->next=NULL;
            return;
        }
        if(index==0){
            node->next=head;
            head=node;
            return;
        }
        Node *current_node=head;
        int count=0;
        while(current_node->next!=NULL&&count<index-1){
            count++;
            current_node=current_node->next;
        }
        if(count==index-1){
            node->next=current_node->next;
            current_node->next=node;
            return;
        }
        return;
    }

    void delete_node(int index) {
        if (head == NULL) {
            return;
        }
        Node *current_node = head;
        int count = 0;
        if (index == 0) {
            head = head->next;
            delete current_node;
            return;
        }
        while (current_node->next != NULL && count < index -1) {
            current_node = current_node->next;
            count++;
        }
        if (count == index - 1 && current_node->next != NULL) {
            Node *delete_node = current_node->next;
            current_node->next = delete_node->next;
            delete delete_node;
        }
    }

    int search(int index){
        if(index<0){
            return -1;
        }
        Node *current_node=head;
        int count=0;
        while(current_node->next!=NULL&&count<index){
            current_node=current_node->next;
            count++;
        }
        if(count==index&&current_node!=NULL){
            return current_node->data;
        }
        return -2;
    }

    void show(){
        Node *current_node=head;
        while(current_node!=NULL){
            //cout<<current_node->data<<" ";
            current_node=current_node->next;
        }
        cout<<endl;
    }

    int getLenth(){
        int len=0;
        Node *current_node=head;
        while(current_node!=NULL){
            len++;
            current_node=current_node->next;
        }
        return len;
    }

};

int main(){
    int N,M;
    cin>>N>>M;
    LinkList *linklist=new LinkList();
    //int *key=new int[M];
    for(int i=1;i<=N;i++){
        Node *node=new Node(i);
        linklist->insert(node,i-1);
    }
    //linklist->show();
    for(int j=0;j<M;j++){
        int i;
        cin>>i;
        linklist->delete_node(i-1);
        //linklist->show();
    }
    int count=N-M;
    cout<<linklist->search((count+1)/2-1)<<endl;
    return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值