2011年浙江大学计算机及软件工程研究生机试真题

题目1001:A+B for Matrices

题目描述:

    This time, you are supposed to find A+B where A and B are two matrices, and then count the number of zero rows and columns.

输入:

    The input consists of several test cases, each starts with a pair of positive integers M and N (≤10) which are the number of rows and columns of the matrices, respectively. Then 2*M lines follow, each contains N integers in [-100, 100], separated by a space. The first M lines correspond to the elements of A and the second M lines to that of B.

    The input is terminated by a zero M and that case must NOT be processed.

输出:

    For each test case you should output in one line the total number of zero rows and columns of A+B.

样例输入:
2 2
1 1
1 1
-1 -1
10 9
2 3
1 2 3
4 5 6
-1 -2 -3
-4 -5 -6
0
样例输出:
1
5
大致题目:就是将两个矩阵a和b相加,然后统计结果矩阵有多少行和列是全0的即可。

#include<iostream>
using namespace std;
int a[100][100],b[100][100],c[100][100];
int main()
{
    int n,m;
    int ans,sum;
    while(cin>>n>>m &&( n||m))
    {
        ans=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                cin>>a[i][j];
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                cin>>b[i][j];
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                c[i][j]=a[i][j]+b[i][j];
        //统计行全0的数量
        for(int i=0;i<n;i++)
        {
            sum=0;
            for(int j=0;j<m;j++)
            {
                sum+=c[i][j];
            }
            if(sum==0)
                ans++;
        }
        //统计列全0的数量
        for(int j=0;j<m;j++)
        {
            sum=0;
            for(int i=0;i<n;i++)
            {
                sum+=c[i][j];
            }
            if(sum==0)
                ans++;
        }
        cout<<ans<<endl;

    }
    return 0;
}

题目1002:Grading

题目描述:

    Grading hundreds of thousands of Graduate Entrance Exams is a hard work. It is even harder to design a process to make the results as fair as possible. One way is to assign each exam problem to 3 independent experts. If they do not agree to each other, a judge is invited to make the final decision. Now you are asked to write a program to help this process.
    For each problem, there is a full-mark P and a tolerance T(<P) given. The grading rules are:
    • A problem will first be assigned to 2 experts, to obtain G1 and G2. If the difference is within the tolerance, that is, if |G1 - G2| ≤ T, this problem's grade will be the average of G1 and G2.
    • If the difference exceeds T, the 3rd expert will give G3.
    • If G3 is within the tolerance with either G1 or G2, but NOT both, then this problem's grade will be the average of G3 and the closest grade.
    • If G3 is within the tolerance with both G1 and G2, then this problem's grade will be the maximum of the three grades.
    • If G3 is within the tolerance with neither G1 nor G2, a judge will give the final grade GJ.

输入:

    Each input file may contain more than one test case.
    Each case occupies a line containing six positive integers: P, T, G1, G2, G3, and GJ, as described in the problem. It is guaranteed that all the grades are valid, that is, in the interval [0, P].

输出:

    For each test case you should output the final grade of the problem in a line. The answer must be accurate to 1 decimal place.

样例输入:
20 2 15 13 10 18
样例输出:
14.0
大致题意:专家进行打分,如果第一个和第二个打分的差值在容忍度之内就取二者平均值;若超出容忍度则由第三个专家参加打分。如果第三个专家的打分和前两个专家中任意一个的差值在容忍度内,就是二者的平均值;如果第三个专家的打分和前两个专家中的差值全部在容忍度内,就取三个专家中最大的分数;如果第三个专家的打分和前两个专家中的差值全部不在容忍度内,就由裁判给分。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
using namespace std;
int maxs(int a,int b,int c)
{
    int tmp;
    tmp=max(a,b);
    tmp=max(tmp,c);
    return tmp;
}
int main()
{
    int p,t,g1,g2,g3,g4;
    double ans;
    while(cin>>p>>t>>g1>>g2>>g3>>g4)
    {
        if(fabs(g1-g2)<=t)
            ans=(g1+g2)*1.0/2;
        else
        {
            if(fabs(g3-g1)<=t&&fabs(g3-g2)>t)
                ans=(g1+g3)*1.0/2;
            else if(fabs(g3-g2)<=t&&fabs(g3-g1)>t)
                ans=(g2+g3)*1.0/2;
            else if(fabs(g3-g1)<=t&&fabs(g3-g2)<=t)
            {
            	ans=maxs(g1,g2,g3);
            } 
            else
            	ans=g4;
            
                

        }
        printf("%0.1lf\n",ans); 
    }
    return 0;

}

题目1004:Median

题目描述:

    Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1={11, 12, 13, 14} is 12, and the median of S2={9, 10, 15, 16, 17} is 15. The median of two sequences is defined to be the median of the non-decreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.
    Given two increasing sequences of integers, you are asked to find their median.

输入:

    Each input file may contain more than one test case.
    Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤1000000) is the size of that sequence. Then N integers follow, separated by a space.
    It is guaranteed that all the integers are in the range of long int.

输出:

    For each test case you should output the median of the two given sequences in a line.

样例输入:
4 11 12 13 14
5 9 10 15 16 17
样例输出:
13
大致题意:输入两个序列找到这两个序列合并后的中间数。

注意:每个序列开头的数字是这个序列的长度,不是序列的元素,当时一直没有看见,浪费好多时间敲打。同时这题只有循环输入才过,不是循环输入就wa,简直了。抓狂

正确代码:

#include<iostream>
#include<string.h> 
using namespace std;
long long c[2100000];
void swap(long long *a,long long *b)
{
	long long t;
	t=*a;
	*a=*b;
	*b=t;
}
void csort(long long *a,long long n) 
{
	for(long long i=0;i<n;i++)
	{
		long long small=i;
		for(long long j=i+1;j<n;j++)
		{
			if(a[j]<a[small])
				small=j;
		}
		if(small!=i)
			swap(&a[i],&a[small]);
	}
}
int main()
{
	long long num=0;
	long long a,b;
	long long ans;
	while(	cin>>a)
	{
	num=0;
	for(long long i=0;i<a;i++)
		cin>>c[num++];
	cin>>b;
	for(long long i=0;i<b;i++)
		cin>>c[num++];
	csort(c,num);
	ans=c[(num-1)/2];
	cout<<ans<<endl;
	}
	
	return 0;
}

当时看错题意写出的代码,虽然与题意不符,但是技巧不错:

#include<iostream>
#include<string.h> 
using namespace std;
char a[100],b[100];
int c[100];
void swap(int *a,int *b)
{
	int t;
	t=*a;
	*a=*b;
	*b=t;
}
void csort(int *a,int n) 
{
	for(int i=0;i<n;i++)
	{
		int small=i;
		for(int j=i+1;j<n;j++)
		{
			if(a[j]<a[small])
				small=j;
		}
		if(small!=i)
			swap(&a[i],&a[small]);
	}
}
int main()
{
	int num=-1,d=0;
	int ans;
	gets(a);//输入一行字符串 
	gets(b); 
	for(int i=0;a[i]!='\0';i++)
	{
		d=0;
		while(a[i]!=' '&&a[i]!='\0')//按照空格将字符串转换成int数组 
		{
			d=d*10+a[i]-'0';
			i++;
		}
		c[++num]=d;
	}		
	for(int j=0;b[j]!='\0';j++)
	{	
			d=0;
			while(b[j]!=' '&&b[j]!='\0')
			{
				d=d*10+b[j]-'0';
				j++;
			}
				c[++num]=d;
	}
	for(int i=0;i<=num;i++)
		cout<<c[i]<<" ";	
	cout<<endl;	
	csort(c,num+1);
  //  for(int i=0;i<=num;i++)
	//	cout<<c[i]<<" ";	
	cout<<endl;	
	if((num+1)%2)
		ans=c[(num+1)/2+1];
	else
		ans=c[(num+1)/2];
	cout<<ans<<endl;
	return 0;
}

题目1005:Graduate Admission

题目描述:

    It is said that in 2011, there are about 100 graduate schools ready to proceed over 40,000 applications in Zhejiang Province. It would help a lot if you could write a program to automate the admission procedure.
    Each applicant will have to provide two grades: the national entrance exam grade GE, and the interview grade GI. The final grade of an applicant is (GE + GI) / 2. The admission rules are:

    • The applicants are ranked according to their final grades, and will be admitted one by one from the top of the rank list.
    • If there is a tied final grade, the applicants will be ranked according to their national entrance exam grade GE. If still tied, their ranks must be the same.
    • Each applicant may have K choices and the admission will be done according to his/her choices: if according to the rank list, it is one's turn to be admitted; and if the quota of one's most preferred shcool is not exceeded, then one will be admitted to this school, or one's other choices will be considered one by one in order. If one gets rejected by all of preferred schools, then this unfortunate applicant will be rejected.
    • If there is a tied rank, and if the corresponding applicants are applying to the same school, then that school must admit all the applicants with the same rank, even if its quota will be exceeded.

输入:

    Each input file may contain more than one test case.
    Each case starts with a line containing three positive integers: N (≤40,000), the total number of applicants; M (≤100), the total number of graduate schools; and K (≤5), the number of choices an applicant may have.
    In the next line, separated by a space, there are M positive integers. The i-th integer is the quota of the i-th graduate school respectively.
    Then N lines follow, each contains 2+K integers separated by a space. The first 2 integers are the applicant's GE and GI, respectively. The next K integers represent the preferred schools. For the sake of simplicity, we assume that the schools are numbered from 0 to M-1, and the applicants are numbered from 0 to N-1.

输出:

    For each test case you should output the admission results for all the graduate schools. The results of each school must occupy a line, which contains the applicants' numbers that school admits. The numbers must be in increasing order and be separated by a space. There must be no extra space at the end of each line. If no applicant is admitted by a school, you must output an empty line correspondingly.

样例输入:
11 6 3
2 1 2 2 2 3
100 100 0 1 2
60 60 2 3 5
100 90 0 3 4
90 100 1 2 0
90 90 5 1 3
80 90 1 0 2
80 80 0 1 2
80 80 0 1 2
80 70 1 3 2
70 80 1 2 3
100 100 0 2 4
样例输出:
0 10
3
5 6 7
2 8

1 4
大致题意:有n个学生,m个学校,每个学校有各自的招生名额,每个学生有k个志愿,ge+gi成绩高,并且在ge+gi相同时ge高的学生先选择学校,如果两个学生分数都一样,那么就不管学校收不收满,只要收了一个就全部都收。最后输出每个学校收的学生编号。

注意:由于输入的数据太多,所以在调试的时间采用输入输出重定向的方式:(但是此时要将循环输入变成一次输入)

//    freopen("datain.txt","r",stdin);
//    freopen("dataout.txt","w",stdout);

在cpp文件所在的文件夹中建立datain.txtdataout.txt会在程序运行后自动生成。

本题注意格式,最后一个元素后面不输出空格!!!以及注意accept的初始化不是0,是-1,,就是这两个点没有注意,wa了至少20次没有发现。再见

#include<iostream>
#include<algorithm>
using namespace std;
struct school{
	int total;//学校招的人数 
}sch[100];
struct student{
	int ge;//ge成绩 
	int gi;//gi成绩 
	int choose[100];//学生的志愿选择 
	int num;//学生的编号 
	int accept;//学生最后选择的学校 
}stu[100];
//先按照ge+gi降序排序,在相同情况下按照ge降序排序,在分数完全相同的情况下按照学生编号升序排序 
int cmp(struct student a,struct student b)
{
	if((a.ge+a.gi)==(b.ge+b.gi)&&a.ge!=b.ge)
		return a.ge>b.ge?1:0;
	if((a.ge+a.gi)!=(b.ge+b.gi)) 
		return (a.ge+a.gi)>(b.ge+b.gi)?1:0;
	if((a.ge+a.gi)==(b.ge+b.gi)&&a.ge==b.ge)
		return a.num<b.num?1:0;
}
//按照学生编号升序排序 
int cmp1(struct student a,struct student b)
{
	return a.num<b.num?1:0;
}
int main()
{
//	freopen("datain.txt","r",stdin);
//	freopen("dataout.txt","w",stdout);
	int n,m,k;
	while(cin>>n>>m>>k)//n个学生,m个学校,k个选择 
	{
//		cin>>n>>m>>k;
		for(int i=0;i<m;i++)
			cin>>sch[i].total;
			
		for(int i=0;i<n;i++)
		{
			cin>>stu[i].ge>>stu[i].gi;
			stu[i].num=i;
			stu[i].accept=-1;
			/*将所有学生选择的学校初始化为-1,不能为0,因为有学校编号为0,
			这样的话即使有人没有被录取但是选择的学校仍然是0,则会发生错误。 
			*/
			for(int j=0;j<k;j++)
				cin>>stu[i].choose[j];
		}
		sort(stu,stu+n,cmp);
		//第一个学生先选择 
		stu[0].accept=stu[0].choose[0];
		sch[stu[0].accept].total--;
		//剩下的学生依次选择 
		for(int i=1;i<n;i++)
		{
			int p,g;
			//学生分数相同的情况 
			if(stu[i].ge==stu[i-1].ge&&stu[i].gi==stu[i-1].gi)
			{
				for( p=0;p<k;p++)
				{
					if(sch[stu[i].choose[p]].total>0)
					{
						stu[i].accept=stu[i].choose[p];
						sch[stu[i].accept].total--;
						break;
					}
				}
				if(p==k)//说明没有大学上就上前一个的大学 
				{
					for(int f=0;f<k;f++)
					{
						if(stu[i-1].accept==stu[i].choose[f])
						{
							stu[i].accept=stu[i-1].accept;
							sch[stu[i].accept].total--;
							break;
						}
							
					}
				}
				else//有大学上但是上的比前一个差 
				{
					for(g=0;g<k;g++)
					{
						if(stu[i-1].accept==stu[i].choose[g])
						{
							break;
						}							
						
					}
					if(g<p)
					{
						stu[i].accept=stu[i-1].accept;
						sch[stu[i].accept].total--;
					}
					
				} 
			}
			//学生分数不完全相同的情况 
			else
			{
				for(int j=0;j<k;j++)
				{
					if(sch[stu[i].choose[j]].total>0)
					{
						stu[i].accept=stu[i].choose[j];
						sch[stu[i].accept].total--;
						break;
					}
					
				}
			}
			
		
		}
		sort(stu,stu+n,cmp1);//为了结果升序输出 
		
		for(int i=0;i<m;i++)
		{	
			int k=0;//为了实现最后一个元素没有空格 
			for(int j=0;j<n;j++)
			{
					if(stu[j].accept==i)
					{
						if(k==0)
						{
							cout<<stu[j].num;//为了实现最后一个元素没有空格 
							k=1;//为了实现最后一个元素没有空格 
						}
							
						else
							cout<<" "<<stu[j].num;
					}
			}
			cout<<endl;
				
		}
	
	
	}
	return 0; 
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值