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初学之使用npm搭建Vue环境
Vue初学之使用npm搭建Vue环境
1414 0
|
缓存 网络协议 网络架构
【计算机网络】第三章 数据链路层(MAC地址 IP地址 ARP协议)
【计算机网络】第三章 数据链路层(MAC地址 IP地址 ARP协议)
1119 1
|
9月前
|
传感器 数据采集 数据可视化
基于STM32的智能家居控制系统设计方案
基于STM32的智能家居控制系统设计方案
|
IDE 开发工具 索引
在Python中安装第三方库
在Python中安装第三方库
3511 156
|
供应链 JavaScript 安全
B/S云门诊系统源码,java云诊所源码,基于Spring Boot、Vue.js构建
云端SaaS架构云门诊系统,基于Spring Boot+Vue开发,支持医保结算,集成预约挂号、诊疗、收费、库存、会员管理等功能,适用于各类基层医疗机构,可打包为C/S桌面应用,支持外接设备免安装配置。
443 10
|
存储 缓存 安全
JUC系列之《volatile关键字:穿透Java内存模型的可见性之剑》
本文深入解析Java中的`volatile`关键字,涵盖其核心特性(可见性与有序性)、底层原理(JMM与内存屏障)、典型使用场景(状态标志、单例模式)及局限性(不保证原子性),帮助开发者正确掌握这一轻量级同步工具,避免并发编程误区。
|
存储 人工智能 Cloud Native
科研+ AI :深势科技,全新科研范式引领者
深势科技作为AI for Science的引领者,专注于微观粒子行为研究,推动材料科学和生命科学领域的创新。通过AI技术,深势科技加速了药物研发、纳米药物微流控等技术的发展,显著提升了计算性能和实验效率。与阿里云合作,利用其云原生计算和存储服务,深势科技实现了资源弹性调度和高效管理,大幅提升了研发效率,服务于超过50万科研用户,并计划建设AI for Science超级实验室,推动更多科技创新。
969 0
|
固态存储 测试技术 图形学
电脑升级有哪些建议?
【10月更文挑战第31天】电脑升级有哪些建议?
1090 4
|
Java 编译器 程序员
Android C/C++开发指南
众所周知,Android作为目前主流的移动终端领域的开发平台,其主要的开发语言就是JAVA。Android借助于JAVA高效、灵活的开发模式,迅速占领了移动互联网开发的半壁江山。基于JDK和Android实现的各种JAVA框架,开发人员可以快速实现各种功能的APP,而且APP的安装和升级维护都实现的方便。
1942 0