TopCoder problem "FingerCounting" used in SRM 390 (Division II Level One)



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.
 

Definition

    
Class:FingerCounting
Method:maxNumber
Parameters:int, int
Returns:int
Method signature:int maxNumber(int weakFinger, int maxCount)
(be sure your method is public)
    
 

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
5)
    
3
99999
Returns: 399998

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=8300

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=11124&pm=8300

Writer:

srbga

Testers:

PabloGilberto , Olexiy , Andrew_Lazarev , ivan_metelsky

Problem categories:

Brute Force, Simple Math, Simple Search, Iteration, Simulation