Problem
A jail has a number of prisoners and a number of treats to pass out to them. Their jailer decides the fairest way to divide the treats is to seat the prisoners around a circular table in sequentially numbered chairs. A chair number will be drawn from a hat. Beginning with the prisoner in that chair, one candy will be handed to each prisoner sequentially around the table until all have been distributed.
The jailer is playing a little joke, though. The last piece of candy looks like all the others, but it tastes awful. Determine the chair number occupied by the prisoner who will receive that candy.
For example, there are prisoners and pieces of candy. The prisoners arrange themselves in seats numbered to . Let's suppose two is drawn from the hat. Prisoners receive candy at positions . The prisoner to be warned sits in chair number .
Function Description
Complete the saveThePrisoner function in the editor below. It should return an integer representing the chair number of the prisoner to warn.
saveThePrisoner has the following parameter(s):
- n: an integer, the number of prisoners
- m: an integer, the number of sweets
- s: an integer, the chair number to begin passing out sweets from
Input Format
The first line contains an integer, , denoting the number of test cases.
The next lines each contain space-separated integers:
- : the number of prisoners
- : the number of sweets
- : the chair number to start passing out treats at
Constraints
Output Format
For each test case, print the chair number of the prisoner who receives the awful treat on a new line.
Sample Input 0
2
5 2 1
5 2 2
Sample Output 0
2
3
Explanation 0
In first query, there are prisoners and sweets. Distribution starts at seat number . Prisoners in seats numbered and get sweets. Warn prisoner .
In the second query, distribution starts at seat so prisoners in seats and get sweets. Warn prisoner .
Sample Input 1
2
7 19 2
3 7 3
Sample Output 1
6
3
Explanation 1
In the first test case, there are prisoners, sweets and they are passed out starting at chair . The candies go all around twice and there are more candies passed to each prisoner from seat to seat .
In the second test case, there are prisoners, candies and they are passed out starting at seat . They go around twice, and there is one more to pass out to the prisoner at seat .
How I solved the problem
# 두 가지 경우로 나누어서 생각해봄. 하나는 (1) 시작시점부터 디저트를 나눠주는 데 끝 까지 가지 못하고 끝나는 경우, 그리고 하나는 (2)시작 시점부터 디저트를 나눠주는 데 끝 시점 이후로 계속 나눠줄 수 있는 경우
(1) 경우
(2) 경우
# (1)의 경우는 시작 시점에서 디저트의 수를 더한 다음 1을 빼주는 경우로 하고..
# (2)의 경우는 전체 디저트 수에서 (사람의 수 - 시작 시점 + 1) 을 빼준 다음에 남은 디저트 수에서 사람의 수를 뺸 나머지를 리턴함. 만약 나머지가 0인경우에는 맨 마지막 시점(전체 사람의 수)을 리턴함
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { // Complete the saveThePrisoner function below. static int saveThePrisoner(int n, int m, int s) { int result = 0; if ((m - (n - s + 1)) > 0) { int firstTryRemain = m - (n - s + 1); result = firstTryRemain % n; if (result == 0) { result = n; } } else { result = s + m - 1; } return result; } private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); int t = scanner.nextInt(); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); for (int tItr = 0; tItr < t; tItr++) { String[] nms = scanner.nextLine().split(" "); int n = Integer.parseInt(nms[0]); int m = Integer.parseInt(nms[1]); int s = Integer.parseInt(nms[2]); int result = saveThePrisoner(n, m, s); bufferedWriter.write(String.valueOf(result)); bufferedWriter.newLine(); } bufferedWriter.close(); scanner.close(); } } | cs |
[출처 : https://www.hackerrank.com ]
'1 Day 1 Algorithms' 카테고리의 다른 글
[2019.03.12] Sequence Equation (0) | 2019.03.12 |
---|---|
[2019.03.11] Circular Array Rotation (0) | 2019.03.11 |
[2019.03.06] Viral Advertisement (0) | 2019.03.06 |
[2019.03.05] Beautiful Days at the Movies (0) | 2019.03.05 |
[2019.03.04] Angry Professor (0) | 2019.03.04 |
댓글