2008年北邮复试上机题目
2008.Problem A.人数统计
Description
今年计算机学院研究生入学复试分多个小组。现在老师需要知道每组复试同学中男生和女生的人数。请你编写程序分别统计出男女生的总人数。
Input
输入的第一行是一个数t(0 < t < 10),表示有t组测试用例。
对于每组输入数据有两行,第一行是一个正整数n(1 < n < 1000),表示参加该组复试的总人数。接下来一行有n个整数(取值为0或1),0代表男生,1代表女生,整数间由一个空格隔开。
Output
对于每组测试用例,输出一行,由两个数组成,用一个空格隔开,分别表示男生和女生的总人数。
Sample Input
2
2
1 0
7
0 1 0 0 1 1 0
Sample Output
1 1
4 3
参考代码:
#include <iostream>
using namespace std;
int main()
{
int T;
cin >> T;
while( T-- )
{
int male = 0;
int female = 0;
int n;
cin >> n;
while( n-- )
{
int num;
cin >> num;
if( num )
female++;
else
male++;
}
cout << male << " " << female << endl;
}
return 0;
}
2008.Problem B.数字统计
Description
给你一个非常大的整数x,(-10^400 <=x<= 10^400),请统计x的每一位,分别输出9,1,2出现的次数.
Input
一个大整数;
Output
一共三行,第一行是9出现的次数,第二行是1出现的次数,第三行是2出现的次数。
Sample Input
912912912910915902
Sample Output
6
5
4
参考代码:
#include <iostream>
using namespace std;
int main()
{
int cnt[10] = {0};
string str;
cin >> str;
if( str[0] == '-')
str.erase(0, 1);
int len = str.size();
for( int i = 0; i < len; i++ )
{
cnt[ str[i]-'0' ]++;
}
cout << cnt[9] << endl;
cout << cnt[1] << endl;
cout << cnt[2] << endl;
return 0;
}
2008.Problem C.统计字母
Description
给定一个只有小写英文字母组成的字符串,串长为n。请你编写程序求出这个字符串中出现次数最多的字母。
Input
输入的第一行为t(0 < t < 10),表示有t组测试用例。
对于每组测试用例,输入有两行。
第一行是一个正整数n( 1 < n < 100)表示字符串的长度。
后面一行是一个长度为n的字符串(只由小写字母组成)。
Output
对于每组测试用例,输出一行,仅输出出现次数最多的字母。
测试用例保证出现次数最多的字母只有一个。
Sample Input
2
5
acmcs
3
zzt
Sample Output
c
z
参考代码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int T;
cin >> T;
int maxalpha = 0;
int maxcnt;
while( T-- )
{
int n;
int cnt[26] = {0};
cin >> n;
string str;
cin >> str;
for( int i = 0; i < n; i++ )
cnt[ str[i] - 'a' ]++;
maxalpha = 0;
maxcnt = 0;
for( int i = 0; i < 26; i++ )
{
if( cnt[i] > maxcnt )
{
maxcnt = cnt[i];
maxalpha = i;
}
}
cout << (char)( maxalpha +'a' ) << endl;
}
return 0;
}
2008.Problem D.二叉树前序遍历
Description
给定一棵有n个结点的二叉树,结点的编号为0~n-1。请你编写程序输出二叉树的前序遍历序列。
Input
输入的第一行是一个正整数t(1 < t < 20),表示有t组测试用例。
对于每组测试用例,第一行是一个整数n(0 < n < 20),表示二叉树结点个数。第二行是一个数r(0≤r≤n-1),二叉树根结点的编号。
后面有n-1行,表示二叉树n-1条边的信息。每行三个数a,b,c,三个数间由空格隔开,其中0≤a,b≤n-1且a≠b, c为0或1。a表示边的起点,b表示边的终点。如果c为0,表示b是a的左儿子;如果c为1,表示b是a的右儿子。
Output
对于每组测试用例输出一行,即:该二叉树的前序遍历序列,两个节点编号之间留一个空格。
Sample Input
2
3
2
2 0 0
2 1 1
7
0
0 1 0
0 2 1
1 3 0
1 4 1
2 5 0
2 6 1
Sample Output
2 0 1
0 1 3 4 2 5 6
Hint
由于是计算机自动判题,请严格按照题目的描述输入输出,不要有任何多余的字符出现,尤其是输出行的行首和行尾都不要有多余的空格
参考代码:
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 100;
typedef struct node
{
int lc;
int rc;
node()
{
initnode();
}
void initnode()
{
lc = -1;
rc = -1;
}
};
node Tree[maxn];
int n;
void initTree()
{
for( int i = 0; i < n; i++ )
Tree[i].initnode();
}
void createTree()
{
int root, child, lr;
int edge = n-1;
while( edge-- )
{
cin >> root >> child >> lr;
if( !lr )
Tree[root].lc = child;
else
Tree[root].rc = child;
}
}
bool firstCase = true;
void preorder( int root )
{
if( root == -1 )
return;
if( firstCase )
firstCase = false;
else
cout << " ";
cout << root;
preorder( Tree[root].lc );
preorder( Tree[root].rc );
}
int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int T;
cin >> T;
int root;
while( T-- )
{
cin >> n;
initTree();//cout << 124;
cin >> root;
createTree();//cout << 3546;
firstCase = true;//for( int i = 0; i < n; i++ ) { cout << i <<": lc = " << Tree[i].lc << " rc = " << Tree[i].rc << endl; }
preorder( root );
cout << endl;
}
return 0;
}
2008.Problem E.科学计算器
Description
给你一个不带括号的表达式,这个表达式只包含加、减、乘、除,请求出这个表达式的最后结果,最后结果一定是整数;
Input
一个数学表达式,只包括数字,数字保证是非负整数,以及五种运算符"+","-","*","/","=";数字和运算符之间有一个或者多个空格,运算符的总数不会超过100,最后以"=“号结尾,表示表达式结束。注意:使用C的同学,在读取字符串的时候请使用scanf(”%s",…);以免不必要的错误。
Output
整数;
Sample Input
1 + 2 + 3 * 6 / 9 =
Sample Output
5
参考代码:
#include <iostream>
#include <stack>
#include <cstdio>
#include <map>
using namespace std;
bool cmp( string op1, string op2 )
{
map<string, int> mp;
mp["+"] = 1;
mp["-"] = 1;
mp["*"] = 2;
mp["/"] = 2;
mp["="] = 0;
return mp[op2] > mp[op1];
}
int cal(int n1, int n2, string op )
{
if( op == "+" )
return n1+n2;
if( op == "-" )
return n1+n2;
if( op == "*" )
return n1+n2;
if( op == "/" )
return n1+n2;
}
int main()
{
int T;
cin >> T;
int number;
string oper;
stack<int> num;
stack<string> op;
while( T-- )
{
while( !num.empty() ) num.pop();
while( !op.empty() ) op.pop();
while( oper != "=")
{
cin >> number;
num.push( number );
cin >> oper;
if( cmp( op.top(), oper) )
op.push( oper );
else
{
string optype = op.top();
op.pop();
int num1 = num.top();
num.pop();
int num2 = num.top();
num.pop();
int result = cal( num1, num2, optype );
num.push( result );
}
}
}
return 0;
}
2008.Problem F.寻找第K小的数
Description
给你n个完全不相同整数(n<=300),每一个数都大于0并且小于1000,请找出第k小的数。
Input
输入包括两行,第一行用空格隔开的两个数 n和k;第二行有n个不行同的数;
Output
输出第k小的数字;
Sample Input
5 3
3 2 5 4 1
Sample Output
3
参考代码:
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 1000;
int num[maxn];
int main()
{
freopen( "in.txt", "r", stdin );
int n, k;
cin >> n >> k;
for( int i = 0; i < n; i++ )
cin >> num[i];
k--;
int pivot = num[0];
int low = 0, high = n-1;
while( low < high )
{
int low_temp = low;
int high_temp = high;
pivot = num[low];
while( low < high )
{
while( num[high] >= pivot && low < high )
high--;
num[low] = num[high];
while( num[low] <= pivot && low < high )
low++;
num[high] = num[low];
}
num[low] = pivot;
if( low < k )
{
low++;
high = high_temp;
}
else if( low > k )
{
high--;
low = low_temp;
}
else
cout << num[low] << endl;
}
return 0;
}
此部分参考链接: 北京邮电大学 2008 上机题目