The problem is from codeforces: http://www.codeforces.com/problemset/problem/102/B
The input can be very large, you might wonder to use long arithmetic via arrays. However, it might be easier than you think. The input contains at most 100000 digits. So the sum of digits is at most 900000, which can be held by an 32-bit integer. You will just need to be careful for the input (do not attempt to convert the string into integer, because you might find overflow).
After first iteration, everything can be handled using integers.
The python solution is as follows:
s = raw_input() i = 0 t = len(s) def getsum(k): u = 0 while k > 0: u += k % 10 k /= 10 return u if t == 1 and int(s) < 10: print 0 else: ss = 0 while i < t: ss += int(s[i]) i += 1 ans = 1 while ss >= 10: ans += 1 ss = getsum(ss) print ans
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
223 wordsloading...
Last Post: Two Simple Equations to Compute PI
Next Post: Codeforces: A. Watermelon