首页 > 试题广场 >

Digital Roots

[编程题]Digital Roots
  • 热度指数:7349 时间限制: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
//同样的操作用递归
#include <stdio.h>
int gao(int n)
{
    int sum=0;
    while(n)
    {
        sum+=n%10;n/= 10;
    }
    if(sum>9) return gao(sum);
    else  return sum;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
        printf("%d\n",gao(n));
    return 0;
}

发表于 2020-04-02 21:06:42 回复(0)
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        int a,b;
        int result=0;
        while(n>=10){
            b = n%10;
            a = n/10;
            n = a+b;
        }
        cout<<n<<endl;
    }
    return 0;
}
发表于 2018-03-10 20:48:29 回复(0)
#include 
using namespace std;
int fn(int x){
    int y=0;
    while(x){
        y+=x%10;
        x/=10;
    }
    int z=y;
    if(z<10)
        return z;
    else if(z>=10)
        return fn(z);
}
int main(){
    int n,m=0;
    while(cin>>n){
        if(n<10){
            cout<<n<<endl;
        }else{
            cout<<fn(n)<<endl;
        }
    }
    return 0;
}
发表于 2020-05-06 17:25:16 回复(0)
又是佩服大家高级用法的一题😯
#include<iostream>
using namespace std;
typedef long long ll;

ll root(ll n)
{
	ll a=0,tmp=0;
	while(n>0)
	{
		a = n%10;
		tmp += a;
		n/=10;
	}
	return tmp;
}

int main()
{
	ll n;
	while(cin>>n)
	{
		if(n==0) break;
		ll res = n;
		while(res>=10)
		{
			res = root(res);
		}
		cout<<res<<endl;
	}
	return 0;
}


发表于 2020-04-14 11:32:23 回复(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)

和楼上比起来我也太呆了吧

#include<iostream>
#include<string>
using namespace std;

string solve(string str){
    while(str.length() > 1){
        long long sum = 0;
        for(int i = 0; i < str.length(); i++)
            sum += str[i] - '0';
        str = "";
        int idx = 0;
        while(sum){
            str += (char)(sum % 10 + '0');
            sum /= 10;
        }
    }
    return str;
}

int main(){
    string str;
    while(cin >> str){
        cout << solve(str) << endl;
    }
    return 0;
}
编辑于 2019-03-15 19:04:20 回复(0)
同简单的递归,不过第一个回答真的强hhh,这个考虑到超出整型范围的大数的运算情况,开始不太清楚递归怎么写,其实很简单
 #include<iostream>
#include<cstring>
using namespace std;
int judge(int* a,int len) {
    int total = 0;
    for (int i = 0; i < len; i++)
        total += a[i];
    if (total / 10 == 0)
        return total % 10;
    else {
        int temp = 0;
        while (total > 0) {
            a[temp++] = total % 10;
            total /= 10;
        }
        return judge(a, temp);
    }
}
int main() {
    char ch[30];
    while (cin >> ch) {
        int total = 0;
        int a[30];
        for (int i = 0; i<strlen(ch); i++)
            a[i] = ch[i] - '0';
        cout<<judge(a, strlen(ch))<<endl;
    }
}

发表于 2019-02-09 12:01:53 回复(0)
while True:
    try:
        string = list(input())
        while len(string) != 1:
            string = list(str(sum(map(int,string))))
        print(string[0])
    except Exception:
        break
编辑于 2018-10-01 17:51:23 回复(0)
#include <iostream>

using namespace std;

int main(){
    int number;
    while (cin >> number){
        int sum=0;
        while (number>0){
            sum += number % 10;
            number /= 10;
            if (number == 0){
                if (sum >= 10){
                    number = sum;
                    sum = 0;
                }
            }
        }
        cout << sum << endl;
    }
} 

发表于 2018-09-15 15:47:36 回复(0)
#include<iostream>

using namespace std;
int sumNumber(int num)
{
    int count=0;
    while(num)
    {
        count+=num%10;
        num=num/10;
    }
    return count;
}
int getDigitalRoot(int number)
{
    if(number<10 && number>0)
        return number;
    int num=number;
    while(num>=10)
    {
        num=sumNumber(num);
        
    }
     return num;   
}
int main()
{
    int N;
    while(cin>>N)
    {
        if(N>0)
            cout<<getDigitalRoot(N)<<endl;
    }
    return 0;
}

发表于 2016-08-20 19:16:33 回复(0)
//数根就是模9的结果,特别的,如果sum%9==0,数根是9.
#include<stdio.h>
int main(){
char ch;
int sum; 
    while(1){
    sum=0;
    while(scanf("%c",&ch)&& ch!='\n'){
    sum+=ch-'0';
}
if(sum==0) break;
if(sum%9==0) sum=9;
else 
 sum=sum%9;
printf("%d\n",sum);
}
return 0;
发表于 2016-03-04 14:40:29 回复(0)
套公式:
return (n+8)%9+1;

发表于 2016-08-14 09:07:01 回复(9)
#include <stdio.h>

int digit_root(int n)
{
    int root = 0;
    while (n)
    {
        root += n % 10;
        n /= 10;
    }
    if (root > 9)
    {
        return digit_root(root);
    }
    else 
    {
        return root;
    }
}

int main()
{
    int n = 0;
    while (scanf("%d", &n) != EOF)
    {
        printf("%d\n", digit_root(n));
    }
    return 0;
}

简单的递归
发表于 2018-03-01 16:51:09 回复(0)
解释一下题目: 就是给你一个数让你求数的数根!什么叫数根? 比如24这个数的数根,就是把24各个位的数加起来!2+4=6 而且6是个位数,于是它就是24的数根。 然后39这个数一样的方法,3+9=12 此时我们发现12不是个位数,所以它不是数根,继续让它各个 位数相加,1+2=3,3是个位数所以3是39的数根。
#include <iostream>
using namespace std;

int digital_root(int n) {
    //当n大于9时,循环计算
    while (n > 9) {
        //新的n
        int new_n = 0;
        //计算n的各个位数之和
        while (n > 0) {
            new_n += n % 10;
            n /= 10;
        }
        //更新n
        n = new_n;
    }
    //返回n
    return n;
}

int main() {
    int num;
    //循环读入整数
    while (cin >> num) {
        //输出整数的数字根
        cout << digital_root(num) << endl;
    }
    return 0;
}

发表于 2023-01-13 22:19:03 回复(0)
#include <iostream>
#include <string>
using namespace std;

int fun(int x) {
    string str = to_string(x);
    int res = 0;
    for (int i = 0; i < str.length(); i++)
        res += str[i] - '0';
    return res > 9 ? fun(res) : res;
}

int main() {
    int a;
    while (cin >> a) {
        cout << fun(a) << endl;
    }

}

编辑于 2024-03-19 21:44:30 回复(0)
#include <iostream>
using namespace std;

int main() {
    int n;
    while (cin >> n) {
        while (n >= 10) {
            int sum = 0;
            while (n) {
                sum += n % 10;
                n /= 10;
            }
            n = sum;
        }
        cout << n << endl;
    }
    return 0;
}

编辑于 2024-02-29 23:15:02 回复(0)
#include <iostream>
using namespace std;
int single_digit(int num){
    int digit = 0;
    while(num>0){
        int remain = num%10;
        num /= 10;
        digit += remain;
    }
    if(digit >= 0 && digit <= 9){
        return digit;
    }else {
        return single_digit(digit);
    }
}
int main(){
    int n;
    while (cin >> n){
        cout << single_digit(n) << endl;
    }
}


编辑于 2024-02-29 15:03:35 回复(0)
//不管怎么样还是磨出来了

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)
#include<bits/stdc++.h>
using namespace std;
int hh(int n)
{
      int sum=0;
      while(n)
      {
         sum+=n%10;
         n/=10;
      }
      if(sum>9) return hh(sum);
      return sum;
}
int main()
{
    int n;
    while(cin>>n)
    {
         cout<<hh(n)<<endl;
    }
    return 0;
}
发表于 2023-06-10 19:40:26 回复(0)
#include <stdio.h>

int main() {
    int a;
    while (scanf("%d", &a) != EOF) { // 注意 while 处理多个 case
        int dr=1000;
        while(dr>=10){
            dr=0;
            while(a>0){
            dr+=a%10;
            a/=10;
        }
        a=dr;
        }
        printf("%d\n",dr);
    }
    return 0;
}
发表于 2023-03-22 17:18:42 回复(0)

问题信息

难度:
67条回答 7745浏览

热门推荐

通过挑战的用户

查看代码