FingerCounting
SRM 390 · 2008-02-02 · by srbga
SRM 390 · 2008-02-02 · by srbga · Brute Force, Simple Math, Simple Search, Iteration, Simulation
Problem Statement
Problem Statement
Your little son is counting numbers with his left hand. Starting with his thumb and going toward his pinky, he counts each finger in order. After counting his pinky, he reverses direction and goes back toward his thumb. He repeats this process until he reaches his target number. He never skips a finger. For example, to count to ten, he would count: thumb, index, middle, ring, pinky, ring, middle, index, thumb, index.
Sadly, one of his fingers hurts and he can only count on it a limited number of times. His fingers are numbered 1 through 5 from thumb to pinky. You are given an int weakFinger, the finger that hurts, and an int maxCount, the maximum number of times he can use that finger. Return the largest number he can count to. If he cannot even begin counting, return 0.
Constraints
- weakFinger will be between 1 and 5, inclusive.
- maxCount will be between 0 and 100000, inclusive.
Examples
0)
2 3 Returns: 15
The first 15 numbers are counted with fingers 1,2,3,4,5,4,3,2,1,2,3,4,5,4,3. He would then have to use finger 2 for the next number, but since he has already used it 3 times, he has to stop.
1)
1 0 Returns: 0
He needs to use his thumb when counting the first number, 1, but it's too weak to be used even once.
2)
5 0 Returns: 4
Even though his pinky cannot be used at all, he can count 1,2,3,4 with the other fingers.
3)
2 48 Returns: 193
4)
5 973 Returns: 7788
Submissions are judged against all 37 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class FingerCounting with a public method int maxNumber(int weakFinger, int maxCount) · 37 test cases · 2 s / 256 MB per case