来源:LeetCode 258  Add Dights

  Question:Given a non-negative integer  num , repeatedly add all its digits until the result has only one digit.

  For example:

     Given  num =  , the process is like:   + =  ,   + =  . Since    has only one digit, return it.

  Follow up:
     Could you do it without any loop/recursion in O(1) runtime?

  分析

  数字根(digital root)是自然数的一种性质,即每个自然数都有一个数字根。数根是将一自然数的各个位数相加(即横向相加),若加完后的值大于等于10的话,则继续将各位数进行横向相加直到其值小于10为止。例如54817的数根为7,因为5+4+8+1+7=25,25大于10则再加一次,2+5=7,7小于10,则7为54817的数字根。

  上面问题即是求一个非负整数的数字根。很容易想到下面这种方法解决问题:

#include<stdio.h>
#include<assert.h> int addDigits(int num)
{
int temp=;
while(num>=)
{
temp+=(num%);
num/=;
}
temp+=num; //不要忽略最高位数
num=temp;
if(num>=)
{
num=addDigits(num);//num仍大于10,则递归调用addDights函数
}
return num;
} int main()
{
int num;
scanf("%d",&num);
assert(num>=); //非负整数断言
printf("%d\n",addDigits(num));
return ;
}

  注意题目的延伸:要求我们不使用循环/递归复杂度O(1)

  这里用到一个求数字根的公式:    

             

  上述公式的文字表述为:0的数字根为0,9的倍数的数字根为9,其他自然数的数字根为其除以9的余数。证明过程点击这里

  

  上述公式可简单表述为:

  

  所以对于延伸的问题我们可以写出解决方法如下:

#include<stdio.h>
#include<assert.h> int addDigits(int num)
{
return +(num-)%; //直接调用公式
} int main()
{
int num;
scanf("%d",&num);
assert(num>=); //非负整数断言
printf("%d\n",addDigits(num));
return ;
}

  

数字根(digital root)的更多相关文章

  1. 如何证明一个数的数根(digital root)就是它对9的余数?

    数根就是不断地求这个数的各位数之和,直到求到个位数为止.所以数根一定和该数模9同余,但是数根又是大于零小于10的,所以数根模9的余数就是它本身,也就是说该数模9之后余数就是数根. 证明: 假设有一个n ...

  2. 树根 Digital root

    数根 (又称数字根Digital root)是自然数的一种性质.换句话说.每一个自然数都有一个数根.数根是将一正整数的各个位数相加(即横向相加),若加完后的值大于等于10的话,则继续将各位数进行横向相 ...

  3. 1. 数字根(Digital Root)

    数字根(Digital Root)就是把一个自然数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这个一位数便是原来数字的数字根.例如: 198的数字根为9(1+9+8=18,1 ...

  4. Digital root(数根)

    关于digital root可以参考维基百科,这里给出基本定义和性质. 一.定义 数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这 ...

  5. digital root问题

    问题阐述会是这样的: Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  6. Codeforces Beta Round #10 C. Digital Root 数学

    C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...

  7. 数学 - SGU 118. Digital Root

    Digital Root Problem's Link Mean: 定义f(n)为n各位数字之和,如果n是各位数,则n个数根是f(n),否则为f(n)的数根. 现在给出n个Ai,求出A1*A2*…*A ...

  8. Digital Root 的推导

    背景 在LeetCode上遇到这道题:Add Digits 大意是给一个数,把它各位数字相加得到一个数,如果这个数小于10就返回,不然继续 addDigits(这个相加得到的数). 题目很简单,但是如 ...

  9. codeforces 10C Digital Root(非原创)

    Not long ago Billy came across such a problem, where there were given three natural numbers A, B and ...

随机推荐

  1. Windows 7下安装部署NodeJs

    第一步  安装NodeJs http://nodejs.org/download/ 下载windows版本的msi文件,双击进行安装即可.安装完毕,默认安装路径为C:\Program Files\no ...

  2. HDU 1848

    http://acm.hdu.edu.cn/showproblem.php?pid=1848 利用计算grundy数组,把一类博弈转化为nim博弈,最后x不为0为先手必胜态 #include < ...

  3. 转:Bat命令学习

    转:http://www.cnblogs.com/SunShineYPH/archive/2011/12/13/2285570.html一.基础语法: 1.批处理文件是一个“.bat”结尾的文本文件, ...

  4. ButterKnife View 注入

    /***************************************************************************************** * ButterK ...

  5. button 浏览器兼容问题

    ie8和360出现的问题 应该加上type属性

  6. xp 安装 win7 64

    1.Win7文件准备 (1)下载Win7 的ISO文件到本机硬盘中 (2)用UltraISO等软件加载ISO文件,将win7目录下的bootmgr和boot文件夹复制到C盘根目录下,并在C盘 根目录下 ...

  7. Java 内部类和匿名类 实现JButton动作 ActionListener类

    import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ControlCircle2 extend ...

  8. 解决SimpleCursorAdapter不能自动更新的问题

    假设场景是这样的:你使用SimpleCursorAdapter显示数据,并监听数据的变化:在数据发生变化的时候,调用cursor的requery,期待UI显示也跟着变化. 但是,你可能会发现,UI并没 ...

  9. C++ list<list<int> >类型的对象遍历

    void listSort(list<list<int> >* initList) { list<list<int> >::iterator itera ...

  10. 简单插入排序(C++版)

    #include <iostream> using namespace std; /** \ Insert Sort * * Key: * * reserve: tm = a[i] * * ...