-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday14.cpp
More file actions
41 lines (31 loc) · 1.32 KB
/
day14.cpp
File metadata and controls
41 lines (31 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*A robotic delivery system is operating in a straight line of houses labeled from 1 to n. The robot starts at the first house with a parcel. Every second, the robot delivers the parcel to the next house in the line. Once it reaches the last house, it changes direction and starts moving back towards the first house. For example, once the robot reaches house n, it delivers to house n - 1, then to house n - 2, and so on. Given two positive integers n (number of houses) and time (number of seconds), return the position of the house where the robot will deliver the parcel after time seconds.
Input Format
The first line contains T, the number of test cases.
Each test case consists of two positive integers n and time.
Constraints
1<=T<=1000
2<=n<=1000
1<=time<=1000
Output Format
For each test case, print the position of the house where the parcel will be delivered after time seconds.*/
#include <iostream>
using namespace std;
int findDeliveryPosition(int n, int time) {
int cycle_length = 2 * (n - 1);
time = time % cycle_length;
if (time < n - 1) {
return time + 1;
} else {
return 2 * (n - 1) - time + 1;
}
}
int main() {
int T;
cin >> T;
while (T--) {
int n, time;
cin >> n >> time;
cout << findDeliveryPosition(n, time) << endl;
}
return 0;
}