您的位置:首页 > 理论基础

2008 BUPT 计算机复试上机题

2011-04-03 13:05 197 查看
1. 人数统计

题目地址:http://acm.scs.bupt.cn/onlinejudge/showproblem.php?problem_id=1305

#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int boy;
int girl;
}Human;
int main() {
int t=0,n=0;
int i=0,j=0;
int tmp=0;
Human* a;
scanf("%d",&t);
a = (Human*)calloc(t,sizeof(Human));
for (i=0; i<t; ++i)
{
a[i].boy=0;
a[i].girl=0;
scanf("%d",&n);
for (j=0; j<n; ++j)
{
scanf("%d",&tmp);
if(!tmp) ++a[i].boy;
else ++a[i].girl;
}
}
for (i=0; i<t; ++i)
{
printf("%d %d/n",a[i].boy,a[i].girl);
}
return 0;
}


2. 数字统计

题目地址:http://acm.scs.bupt.cn/onlinejudge/showproblem.php?problem_id=1306

#include <stdio.h>
int a[27];
char b[11];
char s[101];
// 用26个下标标志abcd……比较好
void to0() {
int i=0;
for(i; i< 26; ++i)
a[i] = 0;
}
int main() {
int t=0;
int n=0;
int i=0,j=0;
char c;
int max;
scanf("%d",&t);
for (i=0; i<t; ++i)
{
to0();
scanf("%d",&n);
scanf("%s",s);
for (j=0; (j<n) && (s[j]!='/0'); ++j)
{
++a[s[j]-'a'];
}
max=0;
for(j=1; j<26; ++j)
if(a[j] > a[max])
max=j;
b[i]=max+'a';
}
for(i=0; i<t; ++i)
printf("%c/n",b[i]);
return 0;
}


3. 二叉树的前序遍历

题目地址:http://acm.scs.bupt.cn/onlinejudge/showproblem.php?problem_id=1307

// 二叉树的前序遍历
#include <stdio.h>
typedef struct {
int left;
int right;
}Node;
Node xnode[21][21];  //  强制清零
int root[21];
void travel(int i, int rt) {
printf("%d",rt);
if(xnode[i][rt].left != -1) {
printf(" ");
travel(i, xnode[i][rt].left);
}
if(xnode[i][rt].right != -1) {
printf(" ");
travel(i, xnode[i][rt].right);
}
}
int main() {
int t,n,a,b,c;
int i,j;
for (i=0 ; i<21; ++i)
{
for (j=0 ; j<21; ++j)
{
xnode[i][j].left = -1;
xnode[i][j].right = -1;
}
}
scanf("%d",&t);
for(i=0; i<t; ++i) {
scanf("%d",&n);  // number of nodes
scanf("%d",&root[i]);  // root
for (j=0; j<n-1; ++j)
{
scanf("%d %d %d",&a,&b,&c);
if(!c)
xnode[i][a].left=b;
else
xnode[i][a].right=b;
}
}
for(i=0; i<t; ++i) {
travel(i, root[i]);
printf("/n");
}
return 0;
}


总结: 08年的北邮上机复试比较简单,自习注意即可。

有一点需要记住: 知道值去寻找其下标很麻烦,通常要遍历整个数组。

知道下标去索引值很简单,时间复杂度为 1.

所以有些题目要有逆向思维的能力,例如题目2中的以数组a[26]中的下标0....25代替字母abcd.... 这样直接简化程序。再如题目 3中以数组的下标表示树的结点,这样就很容易取得每个节点的左右子树。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: