8504. Sum of Digits

Sum of Digits

  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≤101000000). 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.



说明:此题就是不断地对数字按位累加,直到这些数字之和变为个位数为止,又由于输入的数字可能会很大,所以应该使用字符串接收,然后将该字符串转为字符数组,字符数组的每一个元素都是数字,对数字累加,然后将累加的和再更新到该字符数组,直到此字符数组的长度为1(也就是这些数字之和变为个位数)。输出的结果为:上述步骤的步数。

import java.util.Scanner;

public class Test8504 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        char[] chArr = str.toCharArray();
        int result = 0;

        while (chArr.length > 1) {
            int num = 0;
            for (char ch : chArr) {
                num += (int) ch - 48;
            }
            str = Integer.toString(num);
            chArr = str.toCharArray();
            result++;
        }

        System.out.println(result);
        sc.close();
    }
}

原文地址:https://www.cnblogs.com/tangxlblog/p/9973626.html

时间: 2024-09-28 16:28:18

8504. Sum of Digits的相关文章

Codeforces Round #277.5 (Div. 2)C. Given Length and Sum of Digits...(贪心)

传送门 Description You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the d

Sum of Digits is Prime

Sum of Digits is Prime Daoyi Peng August 19, 2014 For an integer $q\geqslant2$ let $s_q(n)$ denote the $q$-ary sum-of-digits function of a non-negative integer $n$, that is, if $n$ is given by its $q$-ary digits expansion $n=\sum\limits_{k=0}^{r} a_k

Codeforces Round #277.5 (Div. 2)——C贪心—— Given Length and Sum of Digits

You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base with

CF 489 C Given Length and Sum of Digits... 贪心

题目链接:http://codeforces.com/problemset/problem/489/C 题目大意:给定位数和各个位的和,问满足条件数字的最大值,最小值. 解题思路:模拟即可.主要是细节判断. 代码: 1 const int inf = 0x3f3f3f3f; 2 const int maxn = 1e2 + 5; 3 int m, s; 4 char ans1[maxn], ans2[maxn]; 5 6 void solve(){ 7 memset(ans1, 0, sizeo

1281. Subtract the Product and Sum of Digits of an Integer

Given an integer number n, return the difference between the product of its digits and the sum of its digits. Example 1: Input: n = 234 Output: 15 Explanation: Product of digits = 2 * 3 * 4 = 24 Sum of digits = 2 + 3 + 4 = 9 Result = 24 - 9 = 15 Exam

[codewars_python]Sum of Digits / Digital Root

Instructions In this kata, you must create a digital root function. A digital root is the recursive sum of all the digits in a number. Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way unti

SPOJ CPCRC1C Sum of Digits

题目连接 题意:计算从a到b每个数每位数字相加的和 code: #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; typedef unsigned long long ll; ll cnt[16]; //cnt[i]表示0 到 最大n位数的个位数的和 ll s[16]; //s[i]表示10的i次方 ll num[1

Codeforces 489C Given Length and Sum of Digits...

m位长度,S为各位的和 利用贪心的思想逐位判断过去即可 详细的注释已经在代码里啦~ //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <cstring> #include <cmath> #include <stack> #include <queu

C. Given Length and Sum of Digits...

http://codeforces.com/contest/489/problem/C 大数就是从最高位,能大就大:小数就是从最低位,能小就小,再处理下最高位为0的情况. 无结果无非一个sum太小,min全为0,一个sum太大,全为9还有剩 1 public class Main { 2 public static void main(String[] args) { 3 Scanner io = new Scanner(System.in); 4 int len = io.nextInt(),