首页 > 试题广场 >

Digital Roots

[编程题]Digital Roots
  • 热度指数:7352 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.     For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.

输入描述:
    The input file will contain a list of positive integers, one per line. 
The integer may consist of a large number of digits. (1 <= input value <= 10^9)


输出描述:
    For each integer in the input, output its digital root on a separate line of the output.
示例1

输入

24
39

输出

6
3
//不管怎么样还是磨出来了

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int sum = 0;
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            String s = in.nextLine();
            String t = s;
            do{  
                sum = 0;
                for(int i=0,len = t.length();i<len;i++){
                    sum+=(t.charAt(i)-'0');
                }
                t = Integer.toString(sum);
            }while(sum>=10);
            System.out.println(sum);
            sum = 0;
        }
    }
}

发表于 2024-02-18 15:33:57 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int x = sc.nextInt();
            int result = NumRoot(x);
            if (result > 0)
                System.out.println(result);
            else
                System.out.println();
        }
    }

    public static int NumRoot(int x) {
        int result;
        if (x > 0 && x < 10) result = x;    //如果只有一位,则本身就是数根
        else {

            //先求每个位上的数之和
            int sum = 0;
            while (x > 0) {
                sum += x % 10;
                x /= 10;
            }

            if (sum > 9) result = NumRoot(sum);     //若求出的数和仍不是一位,递归
            else result = sum;              //若求出的数和只有一位,则得到数根
        }
        return result;
    }

}



发表于 2021-01-30 16:17:24 回复(0)
Java 解法
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int i = scanner.nextInt();
            while (i>=10){
                int sum=0;
                while (i>0){
                    sum+=i%10;
                    i=i/10;
                }
                i=sum;
            }
            System.out.println(i);
        }
    }
}


发表于 2020-03-06 12:30:40 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int n=sc.nextInt();
            System.out.println(root(n));
        }
    }
    
    public static int root(int n){
        String str=n+"";
        char[] arr=str.toCharArray();
        int add=0;
        for(int i=0;i<arr.length;i++)
                add+=arr[i]-'0';
        if(n<10) return n;
        else
            return root(add);
    }
}
发表于 2018-07-26 14:59:20 回复(0)

问题信息

难度:
5条回答 7791浏览

热门推荐

通过挑战的用户

查看代码