复旦大学研究生机试题目解析(2016-2018)

1. 工研院(2018)

1.1 集合交并

题目:

第一题,输入两个集合,分别求其交集和并集中元素的个数,每个集合中可能存在相同的元素,而最终的交集和并集中应该不存在。
例如:
输入:
4 5
3 4 7 3
4 6 3 2 6
输出:
2 5

解题:

显然送分题。

#include <iostream>
#include <set>
using namespace std;

int main() {
	int M, N;
	cin >> M >> N;
	set<int> A,B,C;
	while (M-- > 0) {
		int temp;
		cin >> temp;
		A.insert(temp);
		C.insert(temp);
	}
	while (N-- > 0) {
		int temp;
		cin >> temp;
		B.insert(temp);
		C.insert(temp);
	}
	int count = 0;
	for (auto iter = A.begin(); iter != A.end(); iter++) {
		if (B.find(*iter)!=B.end()) {
			count++;
		}
	}
	cout << count << " " << C.size() << endl;
	return 0;
}

1.2 约数求和

题目:

第二题,输入一个数n,输出前n个数的约数的和。(印象中有1s的时间限制,大数据集可能超时,比如100000000)。
例如:
输入:
7
输出:
41

解题:

这道题先做个函数算出给定数字的约数和,在用一个大循环相加。

#include <iostream>
#include <math.h>
using namespace std;

int countysh(int a) {
	int sum = 1 + a;
	for (int i = 2; i < a; i++) {
		if (a%i == 0)
			sum += i;
	}
	return sum;
}

int main() {
	int input, sum = 1;//1的约数为1;
	cin >> input;
	for (int i = 2; i <= 7; i++) {
		sum += countysh(i);
	}
	cout << sum << endl;
	return 0;
}

1.3 交点

题目:

第三题,求线段交点,输入两组线段端点(整型),求其交点,不相交和无穷交点输出一句话就行,输出交点带小数的。
例如:
输入:
0 0 5 5
0 2 2 0
输出:
1 1

解题:

这道题目倒是有点难度,想了想也没啥好办法,设置成double用数学公式y=kx+b死算好了。

#include <iostream>
#include <math.h>
using namespace std;

int main() {
	double x1, y1, x2, y2, x3, y3, x4, y4;
	cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
	double k1 = (y2 - y1) / (x2 - x1), k2 = (y4 - y3) / (x4 - x3), b1 = y1 - k1*x1, b2 = y3 - k2*x3;
	if (k1 == k2)
		cout << "平行或者重合" << endl;
	else {
		double x = (b2 - b1) / (k1 - k2), y = k1*x + b1;
		co
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值