当前位置: 代码迷 >> 综合 >> 【Codeforces Round#620 (Div. 2)】A. Two Rabbits 题解
  详细解决方案

【Codeforces Round#620 (Div. 2)】A. Two Rabbits 题解

热度:80   发布时间:2023-11-17 23:19:56.0

题目链接:A. Two Rabbits

题目

Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.

He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position x, and the shorter rabbit is currently on position y (x<y). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by a, and the shorter rabbit hops to the negative direction by b.

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YulWT1Em-1581893331659)(/images/20200215/rabbits.png)]

For example, let’s say x=0, y=10, a=2, and b=3. At the 1-st second, each rabbit will be at position 2 and 7. At the 2-nd second, both rabbits will be at position 4.

Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let’s find a moment in time (in seconds) after which the rabbits will be at the same point.

输入

Each test contains one or more test cases. The first line contains the number of test cases t (1≤t≤1000).

Each test case contains exactly one line. The line consists of four integers x, y, a, b (0≤x<y≤109, 1≤a,b≤10^9) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.

The customers are given in non-decreasing order of their visit time, and the current time is 0.

输出

For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.

If the two rabbits will never be at the same position simultaneously, print ?1.

样例

input

5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1

output

2
-1
10
-1
1

题意

已经知道两只兔子的初始位置,两只兔子对着跳,问你两只兔子能不能刚好碰面。

输出碰面所需要的时间,如果不能刚好碰面输出 -1 。

解题思路

因为 a ?往右跳,b ?往左跳,所以如果一开始 a 就在 b 的右边,直接 -1 。

如果 a,b 之间的距离和不能被 a,b 各自一跳的距离的和整除,那么直接 -1 。能整除就输出除数。

代码

#include <iostream>using namespace std;int main() {ios::sync_with_stdio(0); cin.tie(0);int t; cin >> t;while (t--) {int a, b, c, d; cin >> a >> b >> c >> d;if (a > b || (b - a) % (c + d)) cout << "-1" << endl;else cout << (b - a)/(c + d) << endl;}return 0;
}

请多多支持猹的个人博客 H_On 个人小站 啊啊啊~拜托惹 > ~ <
因为猹的小站真的还挺可的,所以那边更新的也比较勤奋,感谢关注~我会努力的(? ?_?)?

  相关解决方案