题解
教学楼
1 条题解
-
0
解题思路
三栋楼最高楼层分别是:图书馆 3 层、综合楼 5 层、艺术楼 9 层。教室在
n层,那么只有最高楼层 ≥ n 的楼才可能。在能装下教室的楼里,要选人流量最小的一栋。做法:从图书馆开始,逐一判断楼层够不够,够的话和人流量最小值比较,不断更新;因为人流量两两不同,最后留下的一定是最小的那栋。
参考代码
#include <iostream> using namespace std; int main() { int n, a, b, c; cin >> n >> a >> b >> c; // 三栋楼最高楼层:图书馆3、综合楼5、艺术楼9 // 在楼层够得着(n<=最高楼层)的楼里,选人流量最小的 int minv = 20001; // 记录最小人流量 int ans = 0; // 0图书馆 1综合楼 2艺术楼 if (n <= 3 && a < minv) { minv = a; ans = 0; } if (n <= 5 && b < minv) { minv = b; ans = 1; } if (n <= 9 && c < minv) { minv = c; ans = 2; } if (ans == 0) cout << "library" << endl; else if (ans == 1) cout << "comprehensive" << endl; else cout << "art" << endl; return 0; }复杂度分析
只判断三次,时间复杂度 O(1),额外空间复杂度 O(1)。
- 1