哪位同学最优秀

来源:互联网 发布:标签机打印软件 编辑:程序博客网 时间:2024/04/28 21:22

每份简历都有一个对应的 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 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 婴儿6个月便秘怎么办 7个月的孩子便秘怎么办 四个月宝宝喜欢吃手怎么办 博瑞智教育是上当了怎么办 我43岁记忆力差怎么办 艾灸灸出的湿疹怎么办 饭店合同到期房东不租怎么办 极端暴力控制不住自己怎么办 苹果已停止访问该网页怎么办 qq登陆后隐藏了怎么办 易班密码忘记了怎么办 老师上课讲错了怎么办 专升本差了一分怎么办 登录不上学信网怎么办 steam被好友删了怎么办 护士继续教育学分证丢了怎么办 护士证到期未延续注册怎么办 学籍和户口不在一起小升初怎么办 定了酒店不能退怎么办 去哪儿网酒店不允许取消怎么办 快递寄送身份证扣海关怎么办 7岁龋齿烂到牙根怎么办 法院判完对方说没钱怎么办 初中填完志愿后怎么办 上海小学借读一年级没有学籍怎么办 学历不高的我该怎么办 没学历的我该怎么办 物业达不到服务标准该怎么办 没有能力的人该怎么办 工作累了腰疼怎么办 机场来早了6小时怎么办 苏宁金融综合评分不足怎么办 苏宁金融秒拒怎么办 微盘账号忘记了怎么办 天府e税忘记密码怎么办 未成年在外面没地方住怎么办? 半框眼镜片掉了怎么办 选修差0.5个学分怎么办 脱产考博社保卡怎么办 幼儿上课不认真听讲怎么办 手机恢复的音频文件打不开怎么办