Sum of Digits

简介: Sum of Digits

8504.

Sum of Digits

time limit per test
2 seconds
memory limit per test
265 megabytes
input
standard input
output
standard output
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit?

Input
The first line contains the only integer n (0≤n≤10100000). It is guaranteed that n doesn't contain any leading zeroes.

Output
Print the number of times a number can be replaced by the sum of its digits until it only contains one digit.

Examples
Input

0

Output

0

Input

10

Output

1

Input

991

Output

3

Note
In the first sample the number already is one-digit − Herald can't cast a spell.

The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.

The third test contains number 991. As one casts a spell the following transformations take place: 991→19→10→1. After three transformations the number becomes one-digit.

分析
翻译:
看完最后一部哈利波特电影后,小杰拉尔德也决定练习魔法。他在父亲的魔法书里发现了一个咒语,可以把任何数字的和变成数字。就在杰拉尔德知道这一点的时候,他遇到了一个数字n。杰拉尔德能对它念多少次咒语,直到这个数字变成一位数?
说明:此题就是不断地对数字按位累加,直到这些数字之和变为个位数为止

def calculate(m):
    new1 = 0
    a_list = list(map(int, str(m)))
    for i in range(0, len(str(m))):
        new1 = new1 + a_list[i]
    return new1


def result(m):
    new2 = m
    for i in range(0, len(str(m))):
        count = 0
        if len(str(m)) > 1:
            new2 = calculate(new2)
            count = count + i + 1
            if len(str(new2)) > 1:
                calculate(new2)
            else:
                print(count)
                break
        else:
            print(0)


digit = int(input())
result(digit)
相关文章
CF489C Given Length and Sum of Digits
CF489C Given Length and Sum of Digits
|
算法 Python
LeetCode 410. Split Array Largest Sum
给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组。设计一个算法使得这 m 个子数组各自和的最大值最小。
144 0
LeetCode 410. Split Array Largest Sum
|
文件存储
Sum of Round Numbers
Sum of Round Numbers
132 0
Sum of Round Numbers
Maximum Subsequence Sum
最大连续子列和问题,在此给出题解 (浙大PTA https://pintia.cn/problem-sets/16/problems/665)
LeetCode之Sum of Two Integers
LeetCode之Sum of Two Integers
117 0
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
786 0
|
人工智能 机器学习/深度学习
1007. Maximum Subsequence Sum (25)
简析:求最大子列和,并输出其首末元素。在线处理,关键在于求首末元素。 本题囧,16年9月做出来过,现在15分钟只能拿到22分,有一个测试点过不了。
974 0
|
索引
Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number.
768 0
|
算法 机器学习/深度学习