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)
相关文章
|
Web App开发 编解码 JavaScript
VUE播放RTSP方案,支持H.265!
VUE播放RTSP方案,支持H.265!如果你问一个前端技术人员,近几年最火的前端框架技术是什么,肯定会有人说VUE,确实VUE凭借其简单特性赢得了大家的喜爱,而近期公司有个项目,需要在VUE框架网页上播放RTSP实时视频。
1938 0
|
11月前
|
存储 Unix C++
c++时间形式转换
【10月更文挑战第29天】在 C++ 中,时间形式转换主要涉及将时间在不同表示形式之间转换,如字符串与 `tm` 结构或 `time_t` 类型之间的转换。常用的基本时间类型包括 `time_t` 和 `tm` 结构,转换函数有 `strftime` 和 `strptime`,可以满足大多数时间处理需求。此外,还可以通过自定义类来扩展时间转换功能。
199 0
|
安全 关系型数据库 MySQL
MySQL装机全攻略:从下载到安全配置的详细指南
出于安全考虑,建议禁止root用户通过远程连接登录MySQL数据库。可以通过修改用户权限或配置防火墙规则来实现。 创建新用户并授权: 根据实际需求,创建具有不同权限的用户账户,并为他们分配必要的数据库和表权限。这样既可以满足业务需求,又可以降低安全风险。
|
监控 Kubernetes 网络协议
异常docker导致 cpu 100
生产问题
1074 0
|
网络协议 安全 Linux
【Centos7系统防火墙使用教程【详解】】
CentOS 7是一种常见的Linux操作系统,防火墙作为网络安全的第一道防线,对于服务器的安全至关重要。本文将介绍CentOS 7系统中防火墙的使用教程,包括如何开启、关闭、配置以及防火墙规则的添加和删除。
2407 0
|
缓存 Linux 开发工具
详细的Git使用教程
Git是一款免费、开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目版本管理。
774 0
详细的Git使用教程
|
Java 分布式数据库 API
HbaseJAVA开发API导入jar包以及实现操作命令
HbaseJAVA开发API导入jar包以及实现操作命令
579 0
HbaseJAVA开发API导入jar包以及实现操作命令
|
运维 Kubernetes 算法
独家深度 | 那些年我做开源和自研走过的弯路和经验
本文将重点分享开源结合自研项目的一些经验。
950 1
独家深度 | 那些年我做开源和自研走过的弯路和经验
|
消息中间件 设计模式 移动开发
最全的前端面试题、后端面试题、包含大厂面试题2022年最新
整理了一下前端和java后端的面试及大厂面试题,都是我自己自用的,比较珍贵,整理不易。
663 0
最全的前端面试题、后端面试题、包含大厂面试题2022年最新
|
存储 弹性计算 运维
云上的潮玩社区,如何让年轻人念念不忘?
许多潮玩品牌正是看中千岛品牌资源整合的能力,首选在千岛微信小程序上进行新品首发、新品市场测试等业务。由于这些业务流量的激增,在业务架构上,如何有效应对突发流量、保障业务系统稳定性成了至关重要的问题。
1427 0
云上的潮玩社区,如何让年轻人念念不忘?