Soluție HackerRank pentru Reverse Game, subdomeniul Fundamentals, în C++14. Include cerința formatată, exemple, explicația pașilor și cod sursă.
- Problemă: Reverse Game
- Domeniu: Fundamentals
- Limbaj: C++14
Challenge: Reverse Game
Subdomeniu: Fundamentals (fundamentals)
Scor cont: 30.0 / 30
Submission status: Accepted
Submission score: 1.0
Submission ID: 464726749
Limbaj: cpp14
Link challenge: https://www.hackerrank.com/challenges/reverse-game/problem
Cerință
Akash and Akhil are playing a game. They have N balls numbered from 0 to N-1. Akhil asks Akash to reverse the position of the balls, i.e., to change the order from say, 0,1,2,3 to 3,2,1,0. He further asks Akash to reverse the position of the balls N times, each time starting from one position further to the right, till he reaches the last ball. So, Akash has to reverse the positions of the ball starting from 0-th position, then from 1^st position, then from 2^nd position and so on. At the end of the game, Akhil will ask Akash the final position of any ball numbered K. Akash will win the game, if he can answer. Help Akash.
Input Format
The first line contains an integer T, i.e., the number of the test cases.
The next T lines will contain two integers N and K.
Output Format
Print the final index of ball K in the array.
Constraints
1 ≤ T ≤ 50
1 ≤ N ≤ 10^5
0 ≤ K < N
Sample Input
2
3 1
5 2
Sample Output
2
4
Explanation
For first test case, The rotation will be like this:
0 1 2 -> 2 1 0 -> 2 0 1 -> 2 0 1
So, Index of 1 will be 2.
Cod sursă
#include <iostream>
using namespace std;
int main()
{
int t, n, k;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n >> k;
k <= n / 2 - 1 ? cout << 2 * k + 1 << endl : cout << 2 * (n - 1 - k) << endl;
}
return 0;
}
