Digital Root(九度教程第 42 题);

Digital Root(九度教程第 42 题)

1.题目描述:

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.

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

样例输入:
24
39
样例输出:
6
3

2.基本思路

有递归和迭代两种方法,基本思路就是对数进行数位拆解,并将每个拆解出来的数字进行求和。判断其是否小于10,否则继续进行拆解。

3.代码实现

#include <iostream>

using namespace std;

int F_recursive(int x){//递归
    if(x<10)return x;
    if(x>=10){
        int num =x;
        int res=0;
        while(num!=0){
            res+=num%10;
            num/=10;
        }
        F_recursive(res);
    }

}
int F_iterator(int x){
    int num=x;
    int tmp;
    while(num>9){
        tmp=0;
        while(num!=0){
          tmp+=num%10;
          num/=10;
        }
        num=tmp;
    }
    return num;
}

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

4.问题反思

问题看似简洁,在小规模的案例上都通过了。但是!!!在hdu的virtual judge上面,Wrong answer!!!,找了一会也没发现问题,最后发现问题没有声明给定的数据的范围,只知道是一个正整数,凉凉坑定是大数越了int类型的界了。现在只能用char[]数组来存储数值了。这里新加一个函数用于计算数组中的元素之和,然后把各元素之和传给之前写好的迭代或递归函数即可。

#include <iostream>
#include <string.h>
#define N 10000
using namespace std;

int F_iterator(int x){
    int num=x;
    int tmp;
    while(num>9){
        tmp=0;
        while(num!=0){
          tmp += num%10;
          num/=10;
        }
        num=tmp;
    }
    return num;
}

int F(char num[]){
    int l = strlen(num);
    int res=0;
    for(int i=0;i<l;i++){
        res += num[i]-'0';
    }
    if(res>=10)
        return F_iterator(res);
    else
        return res;
}

int main()
{
    char num[N];
    while(scanf("%s",num)!=EOF){
        if(num[0]=='0')break;//注意在hdu上面这里有个坑,即0,00,01,001都是结束条件,没有规定必须是0,所以不能在if里面加上strlen(num)==1,博主就吃过这个亏
        printf("%d\n",F(num));
    }
    return 0;
}

这里关于Digital Root还有一个计算公式:

树 根 公 式 : d = ( n − 1 ) % 9 + 1 ; 树根公式: d=(n-1)\%9+1; :d=(n1)%9+1;
其中n为输入数字的各个位数之和。(这里不给出证明)
采用以上公式可以使得F_iterator函数复杂度之间降为O(1).

int F_iterator(int x){
    return (x-1)%9+1;
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值