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

題目鏈接: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 個人小站 啊啊啊~拜託惹 > ~ <
因爲猹的小站真的還挺可的,所以那邊更新的也比較勤奮,感謝關注~我會努力的(ง •_•)ง

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章