Connection Status:
Competition Arena > AdditionCycles
SRM 326 · 2006-11-11 · by jmzero · Simple Math
Class Name: AdditionCycles
Return Type: int
Method Name: cycleLength
Arg Types: (int)
Problem Statement

Problem Statement

Start with any integer between 00 and 99, inclusive, written as two digits (use a leading zero if the number is less than 10). Add the two digits together. Now concatenate the rightmost digit of the first number with the rightmost digit of the sum to get a new number. If you repeat this process enough times, you'll end up back at the original number. For example:


			    Combine Second Digits of
Start With    Add Digits    the Original and the Sum
------------------------------------------------------
    26     :   2+6 = 08   :   "6" and "8" = 68 
    68     :   6+8 = 14   :   "8" and "4" = 84
    84     :   8+4 = 12   :   "4" and "2" = 42
    42     :   4+2 = 06   :   "2" and "6" = 26

In this case, it took us 4 steps to get back to where we started, so we would return 4. Starting with n, return the number of steps it takes to get back to n.

Constraints

  • n will be between 0 and 99, inclusive.
Examples
0)
26
Returns: 4

The example from the problem statement. It goes 26->68->84->42->26, so there's 4 steps for the cycle.

1)
55
Returns: 3

The cycle is 55->50->05->55. Remember to treat numbers under 10 as though there was a leading zero.

2)
0
Returns: 1

Zero comes back to zero at every step - so the length of the cycle is one (00->00)

3)
71
Returns: 12
4)
1
Returns: 60

Submissions are judged against all 110 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class AdditionCycles with a public method int cycleLength(int n) · 110 test cases · 2 s / 256 MB per case

Submitting as anonymous