北邮计算机院上机.docx
- 文档编号:10042145
- 上传时间:2023-02-08
- 格式:DOCX
- 页数:10
- 大小:16.92KB
北邮计算机院上机.docx
《北邮计算机院上机.docx》由会员分享,可在线阅读,更多相关《北邮计算机院上机.docx(10页珍藏版)》请在冰豆网上搜索。
北邮计算机院上机
本贴原文地址为:
题A
Description
比较奇偶数的个数。
Input
第一行是一个正整数n,表示一共有n个数。
第二行为n个整数。
Output
n个整数中奇数多则输出YES,否则输出NO。
SampleInput
5
12345
SampleOutput
YES
题B
Description
在平面上的n个点中找出横坐标最小的点。
Input
第一行是一个正整数n(1<=n<=1000),表示一共有n个点。
接下来n行,每行2个整数,分别表示每个点的横坐标和纵坐标。
Output
输出横坐标最小的那个点的横坐标和纵坐标,如果横坐标最小的点有多个,则只输出其中纵坐标最小的那个点。
SampleInput
5
52
23
24
32
41
SampleOutput
23
题C
Description
子矩阵旋转操作。
Input
前5行每行5个整数,表示一个5*5阶的矩阵。
第6行为4个整数abcd,根据这4个整数进行相应的旋转操作。
Output
若ab分别为12,则将原矩阵中以第c行第d列元素为左上角的2*2阶子矩阵顺时钟旋转90度后输出原矩阵。
若ab分别为13,则将原矩阵中以第c行第d列元素为左上角的3*3阶子矩阵顺时钟旋转90度后输出原矩阵。
若ab分别为22,则将原矩阵中以第c行第d列元素为左上角的2*2阶子矩阵逆时钟旋转90度后输出原矩阵。
若ab分别为23,则将原矩阵中以第c行第d列元素为左上角的3*3阶子矩阵顺时钟旋转90度后输出原矩阵。
SampleInput
12345
678910
1112131415
1617181920
2122232425
1311
SampleOutput
116145
1272910
13831415
1617181920
2122232425
题D
Description
赫夫曼编码
给出n个有权值的结点,构造赫夫曼树,输出所有这n个结点的权值与其赫曼编码长度(即该结点在所构造的赫曼树中的深度)的乘积的总和。
Input
第一行是一个正整数n,表示一共有n个结点。
第二行为n个整数,分别表示这n个结点的权值。
Output
输出所有这n个结点的权值与其赫曼编码长度的乘积的总和。
SampleInput
5
12259
SampleOutput
37
PS:
北邮计算机学院的复试上机测试是2个小时4道题,前3题稍简单,最后一道有点难度。
但今年最后关于赫夫曼编码的那道题与09年的最后一道基本相同,复试前如果练过09年的上机题的话基本做今年的这道题应该没有问题,据说09年4道全AC的全院只有4个,今年又几乎原封不动的考了这道题,4道全AC的全院有30个左右。
个人感觉北邮复试机试和面试比较重要,听力和专业课笔试基本上是走过场,专业课复试前也就看了半天编译原理(实在看不下去),基本是裸考。
以下是上面4道题的C++代码,都能AC。
代码除了注释外都是复试时候写的(题D为了在VC上也能编译,稍微改动了一下),由于时间关系,考试时并没有考虑效率问题,只是想尽快AC就行,所以这些代码并不一定是效率最好的,应该还有不少改进的余地。
=================================
题A
#include
usingnamespacestd;
intmain()
{
inteven=0,odd=0;
intn;
cin>>n;
intnum;
for(inti=0;i cin>>num; if(num%2) odd++; else even++; } if(odd>even) cout<<"YES\n"; else cout<<"NO\n"; return0; } ================================= 题B #include #include #include usingnamespacestd; structnode{ intx; inty; }; boolcmpx(node*a,node*b){ returna->x } boolcmpy(node*a,node*b){ returna->y } intmain() { intn; cin>>n; vector for(inti=0;i node*nd=newnode; cin>>nd->x>>nd->y; nvec.push_back(nd); } sort(nvec.begin(),nvec.end(),cmpy); stable_sort(nvec.begin(),nvec.end(),cmpx); cout< return0; } ================================= 题C #include usingnamespacestd; intmain() { intalt[5][5]; intcmdx,cmdy,x,y; for(inti=0;i<5;i++) for(intj=0;j<5;j++) cin>>alt[i][j]; cin>>cmdx>>cmdy>>x>>y; if(cmdx==2) if(cmdy==2){ int*arr=newint[4]; arr[0]=alt[x-1][y-1]; arr[1]=alt[x-1][y]; arr[2]=alt[x][y-1]; arr[3]=alt[x][y]; alt[x-1][y-1]=arr[1]; alt[x-1][y]=arr[3]; alt[x][y-1]=arr[0]; alt[x][y]=arr[2]; } else{ int*arr=newint[9]; arr[0]=alt[x-1][y-1]; arr[1]=alt[x-1][y]; arr[2]=alt[x-1][y+1]; arr[3]=alt[x][y-1]; arr[4]=alt[x][y]; arr[5]=alt[x][y+1]; arr[6]=alt[x+1][y-1]; arr[7]=alt[x+1][y]; arr[8]=alt[x+1][y+1]; alt[x-1][y-1]=arr[2]; alt[x-1][y]=arr[5]; alt[x-1][y+1]=arr[8]; alt[x][y-1]=arr[1]; alt[x][y]=arr[4]; alt[x][y+1]=arr[7]; alt[x+1][y-1]=arr[0]; alt[x+1][y]=arr[3]; alt[x+1][y+1]=arr[6]; } else if(cmdy==2){ int*arr=newint[4]; arr[0]=alt[x-1][y-1]; arr[1]=alt[x-1][y]; arr[2]=alt[x][y-1]; arr[3]=alt[x][y]; alt[x-1][y-1]=arr[2]; alt[x-1][y]=arr[0]; alt[x][y-1]=arr[3]; alt[x][y]=arr[1]; } else{ int*arr=newint[9]; arr[0]=alt[x-1][y-1]; arr[1]=alt[x-1][y]; arr[2]=alt[x-1][y+1]; arr[3]=alt[x][y-1]; arr[4]=alt[x][y]; arr[5]=alt[x][y+1]; arr[6]=alt[x+1][y-1]; arr[7]=alt[x+1][y]; arr[8]=alt[x+1][y+1]; alt[x-1][y-1]=arr[6]; alt[x-1][y]=arr[3]; alt[x-1][y+1]=arr[0]; alt[x][y-1]=arr[7]; alt[x][y]=arr[4]; alt[x][y+1]=arr[1]; alt[x+1][y-1]=arr[8]; alt[x+1][y]=arr[5]; alt[x+1][y+1]=arr[2]; } for(intii=0;ii<5;ii++){ for(intjj=0;jj<4;jj++) cout< cout< } return0; } ================================= 题D #include #include #include usingnamespacestd; intlen=0; structnode{ intvalue; intdepth; node*left; node*right; booloperator<(node&a){returnvalue }; voidcalculate(node*n){ if(n! =0){ n->depth=len; len++; calculate(n->left); calculate(n->right); len--; } } voidgetSum(node*n,int&s){ if(n! =0){ if(n->left==0&&n->right==0) s+=(n->depth)*(n->value); if(n->left! =0) getSum(n->left,s); if(n->right! =0) getSum(n->right,s); } } intmain() { intn; intsum=0; cin>>n; list for(inti=0;i node*nd=newnode; cin>>nd->value; nd->left=nd->right=0; nlist.push_back(nd); } nlist.sort(); node*root; while(nlist.size()>1){ node*nd1=nlist.front(); nlist.pop_front(); node*nd2=nlist.front(); nlist.pop_front(); node*nd=newnode; nd->value=nd1->value+nd2->value; nd->left=nd1; nd->right=nd2; list : iteratorit; for(it=nlist.begin();it! =nlist.end();it++) if((*it)->value>nd->value) break; nlist.insert(it,nd); } root=nlist.front(); calculate(root);//遍历一次赫夫曼树,计算出每个叶子结点的深度 getSum(root,sum);//又遍历了一次赫夫曼树,计算出所求总和,实际上应该只需遍历一次即可 cout< return0; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 上机