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

最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



