CCCHK '08 J2 - Lucky Number

View as PDF

Submit solution

Points: 3
Time limit: 1.0s
Memory limit: 64M

Problem type

In ancient Europe, people believed that their luck was dependent on a number. By summing up the digits of their birthday, they got a sum. By repeatedly adding the digits of the sum until a single digit number remains. This resultant number was called the "single digit representation". And the digit reflected their luck in their whole life.

In this question, a birthday will be given by a non-negative integer x (\le 10\,000 digits). Your program has to compute the single digit representation of the given number. Example:

1 \to 1
10 \to 1+0 = 1
19 \to 1+9 = 10 \to 1
999 \to 9+9+9 = 27 \to 9

Input Specification

The first input is an integer specifying the number of test cases. Then each input number appears on a line by itself.

Output Specification

For each test case, output the single digit representation of it.

Sample Input

4
1
10
19
999

Sample Output

1
1
1
9

Comments


  • -1
    jedidiah  commented on Sept. 5, 2024, 7:28 a.m. edited

    For Python users, start with this code:

    import sys
    input = sys.stdin.readline
    

    It makes it much faster.


  • 3
    Fares_X  commented on Dec. 10, 2022, 7:59 p.m.

    This kind of number is better known as "digital root" (999, anyone?)


  • 4
    Goat_dy  commented on Aug. 27, 2019, 7:19 p.m.

    Why do I keep getting IRs?


    • 6
      hxxr  commented on Aug. 27, 2019, 8:22 p.m. edit 2

      The input integers can be up to 10\,000 digits long. Integer.parseInt() cannot parse integers from the input greater than 2\,147\,483\,647.

      And in fact you cannot store a 10\,000 digit number as an int or even a long. Consider storing it as a String.


  • -5
    cardistryMagic  commented on March 14, 2017, 3:40 p.m. edited

    This comment is hidden due to too much negative feedback. Show it anyway.


    • 7
      Xyene  commented on March 14, 2017, 4:30 p.m.

      You're trying to read an integer of maximum length 10\,000 digits. That's far greater than the 2^{31}-1 max value of an int.


      • 2
        Sealeome1  commented on Aug. 1, 2021, 10:18 p.m.

        oh. I didn't know that. Thank you!