洛谷 P8741 [蓝桥杯 2021 省 B] 填空问题 题解

题目分析

A 空间

简单的单位换算题:
1 MB = 1024 KB = 1048576 B = 8388608 b 256 × 8388608 ÷ 32 = 67108864 1\text{MB}=1024\text{KB}=1048576\text{B}=8388608\text b\\ 256\times8388608\div32=67108864 1MB=1024KB=1048576B=8388608b256×8388608÷32=67108864

故答案为 67108864 67108864 67108864。

B 卡片

用一个数组存储每种卡片剩余的数量,再从 1 1 1 开始,一个个判断每个数需要的卡片是否够用,如果不够用了就输出结果。最终可以得到答案为 3181 3181 3181。

C 直线

枚举每一种组合然后去掉重复的部分即可。

对于去重,可以使用 set 来实现。

代码如下:

cpp 复制代码
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
signed main() {
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	vector<pair<int,int>>vec;
	set<pair<pair<int,int>,int>>ans;
	for(int i=1;i<=20;++i) for(int j=1;j<=21;++j) vec.push_back({i,j});//枚举组合
	for(int i=0;i<vec.size()-1;++i)
		for(int j=i+1;j<vec.size();++j) {
			int x1=vec[i].first,y1=vec[i].second,x2=vec[j].first,y2=vec[j].second;
			int a=x2-x1,b=y1-y2,c=x1*y1-x2*y2;
			int gcd=__gcd(__gcd(a,b),c);
			ans.insert({{b/gcd,a/gcd},c/gcd});//去重
		}
	cout<<ans.size();//输出答案
	return 0;
}

得到答案为 40257 40257 40257。

D 货物摆放

计算出 n n n 的所有因数储存起来,再枚举所有的组合,如果乘积刚好等于 n n n,就累加答案。最终可以得到答案为 2430 2430 2430。

E 路径

就是一个求最短路径的板子题,可以使用 Dijkstra 算法。最终得到答案是 10266837 10266837 10266837。

Code

cpp 复制代码
#include<iostream>
using namespace std;
signed main() {
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	string ans[]={
		"67108864",
		"3181",
		"40257",
		"2430",
		"10266837"
	};
	cout<<ans[getchar()-'A'];
	return 0;
}
相关推荐
樱木Plus19 小时前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
blasit3 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_4 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星4 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛6 天前
delete又未完全delete
c++
端平入洛7 天前
auto有时不auto
c++
哇哈哈20217 天前
信号量和信号
linux·c++
多恩Stone7 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马7 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝8 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode