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();
    }
}

 

posted @ 2018-11-17 13:25  人工智能之路上的菜鸡  阅读(521)  评论(0编辑  收藏  举报