Thermostat(思维)
题目描述:
Vlad came home and found out that someone had reconfigured the old thermostat to the temperature of
The thermostat can only be set to a temperature from
You are given
输入描述:
The first line of input data contains the single integer
The descriptions of the test cases follow.
The first line of each case contains three integers
The second line of each case contains two integers
输出描述:
Output
Note:
In the first example, the thermostat is already set up correctly.
In the second example, you can achieve the desired temperature as follows:
In the third example, you can achieve the desired temperature as follows:
In the fourth test, it is impossible to make any operation.
AC代码:
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int N = 1e5 + 10; int T; int l, r, x, a, b; void solve() { cin >> l >> r >> x >> a >> b; int minn = min(a, b); // 找出 a 和 b 中的最小值 int maxx = max(a, b); if (a == b) // 如果 a 和 b 相等则不用移动 { cout << 0 << '\n'; return; } if (abs(a - b) >= x) // 如果 a 和 b 的差大于等于 x 则直接将 a 移动到 b { cout << 1 << '\n'; return; } if (maxx + x <= r || minn - x >= l) // 若最大值加上 x 的值仍然在 r 的范围内或者最小值减去 x 的值仍然在 l 的范围内,那么就代表可以只用移动两次 { cout << 2 << '\n'; return; } if (minn + x > b && minn + x <= r && maxx - x < a && maxx - x >= l) // 若最小值加上 x 的值在 b 和 r 的范围内并且最大值减去 x 的值在 l 和 a 的范围内,则可以先将最小值和最大值加上减去 x,此时两点的距离必大于 x,需要移动三步 { cout << 3 << '\n'; return; } cout << -1 << '\n'; // 上述几种情况都不满足则代表无法从 a 移动到 b } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); // scanf("%d", &T); cin >> T; while (T--) { solve(); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话