哪位同学最优秀
来源:互联网 发布:标签机打印软件 编辑:程序博客网 时间: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&¤t_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&¤t_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
- 哪位同学最优秀
- 哪位老师或同学给解答下,谢谢
- 杭电2028,2030,哪位同学给个帮助吧!
- 男人什么地方最优秀
- 徐晓明同学迄今最认真的学员
- 同学最擅长的开辟数组空间
- 想在短时间内(两周)了解一下数据库,有哪位同学给推荐一个好点的教材?
- 哪位高手有.net2008的优秀教程,发一个给我
- 最差劲的同学现在最富有---励志篇
- 同学
- 最优秀的人是谁
- 中国最优秀的经理
- 最优秀的ajax框架
- 谁是最优秀的人?
- 最优秀的就是你自己
- 什么样的人最优秀
- 为什么我们不是最优秀?
- 如何才能找到最优秀开发者?
- 比赛记录
- Fragment的生命周期
- 在centos 上安装jenkins
- java中的final关键字的作用
- struts封装表单数据
- 哪位同学最优秀
- 第六次C++上机实验
- appium 安卓自动化环境搭建
- Linux crontab 命令格式与详细例子
- WebView
- Redis安装
- 最长公共子序列
- 易生活(二)-APP—安卓中评论功能的实现
- bzoj 1295: [SCOI2009]最长距离 dfs