数根

(又称数字根Digital
root)是自然数的一种性质。换句话说。每一个自然数都有一个数根。数根是将一正整数的各个位数相加(即横向相加),若加完后的值大于等于10的话,则继续将各位数进行横向相加直到其值小于十为止,或是,将一数字反复做数字和,直到其值小于十为止,则所得的值为该数的数根。

比如54817的数根为7。由于5+4+8+1+7=25,25大于10则再加一次。2+5=7,7小于十。则7为54817的数根。

百度百科:http://baike.baidu.com/link?

url=FKQ337jynzKVjYV7X92BZOWW51nI6unO71jOTV1g7gnGKnChCWXkNHB4hqTUCmvrwbPh9voBvMAZcxca3ohAua

维基百科:https://en.wikipedia.org/wiki/Digital_root

leetcode: https://leetcode.com/problems/add-digits/

leetcode原题:

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

For example:

Given num = 38, the process is like: 3
+ 8 = 11
1 + 1 = 2. Since 2 has
only one digit, return it.

Follow up:

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

解题思路一:

将数的每一位取出来再相加。再将和的每一位取出来相加。直到和为一个个位数。

代码例如以下:

class Solution {
public:
int addDigits(int num) {
if(num/10==0) return num;
int res=0;
while(num/10!=0)
{
res=0;
while(num)
{
int temp=num%10;
res+=temp;
num/=10;
}
num=res;
} return res;
}
};

解题方法二:

採用有限域的相关知识:

代码例如以下:

class Solution {
public:
int addDigits(int num) {
return 1+(num-1)%9;
}
};

树根 Digital root的更多相关文章

  1. Digital root(数根)

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

  2. 数字根(digital root)

    来源:LeetCode 258  Add Dights Question:Given a non-negative integer  num , repeatedly add all its digi ...

  3. 【HDOJ】4351 Digital root

    digital root = n==0 ? 0 : n%9==0 ? 9:n%9;可以简单证明一下n = a0*n^0 + a1*n^1 + ... + ak * n^kn%9 = a0+a1+..+ ...

  4. Sum of Digits / Digital Root

    Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...

  5. digital root问题

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

  6. 1. 数字根(Digital Root)

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

  7. 快速切题 sgu118. Digital Root 秦九韶公式

    118. Digital Root time limit per test: 0.25 sec. memory limit per test: 4096 KB Let f(n) be a sum of ...

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

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

  9. 数学 - SGU 118. Digital Root

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

随机推荐

  1. swift语言点评四-Closure

    总结:整个Closure的作用在于简化语言表述形式. 一.闭包的简化 Closure expression syntax has the following general form: { () -& ...

  2. Node_进阶_7

    Node进阶第七天 一.复习 一.索引   数据库中,根据一个字段的值,来寻找一个文档,是很常见的操作.比如根据学号来找一个学生.这个学号是唯一的.只要有学号,就能唯一确认一个学生的文档.学号这个属性 ...

  3. caioj 1152 快速求模 (快速幂)

    (1)开long long,不然中间结果会溢出 (2)注意一开始的初始化,保险一点. #include<cstdio> #include<cctype> #include< ...

  4. Matlab 图像的邻域和块操作

    图像的邻域操作是指输出图像的像素点取值,由输入图像的某个像素点及其邻域内的像素,通常像素点的邻域是一个远小于图像本身尺寸.形状规则的像素块,如2×2,3×3正方形.2×3矩形等,或者近似圆形的多边形. ...

  5. Qt之字体文件(TTF)

    简述 TTF(TrueTypeFont)是Apple公司和Microsoft公司共同推出的字体文件格式,随着windows的流行,已经变成最常用的一种字体文件表示方式. 在一些特殊的场合,系统字符集不 ...

  6. C#获取本机内网外网IP

    using System.Net; # region 获取内.外网Ip /// <summary> /// 获取本地ip地址,优先取内网ip /// </summary> pu ...

  7. Unity 相机花式分屏

    花式分屏,顾名思义,可以实现各种不规则几何边界的分屏,是无法直接通过调整相机视口能达到效果的(只能实现矩形的分屏),例如斜对角分屏,几何图形分屏: 假设我们有两个相机,需要上面的斜对角分屏画面,和镜子 ...

  8. <a>标签是什么意思 怎么使用?

    转自:https://www.imooc.com/qadetail/190881 (1) a标签的作用:超链接,用于跳转到别的网页. (2) a标签的用法:<a href="网址&qu ...

  9. django笔记10 cookie整理

    感谢武沛齐老师 Alex老师 cookie 没有cookie所有的网站都登录不上 客户端浏览器上的一个文件 {'user':'ljc'} {"user":'zpt'} reques ...

  10. SPFA的两种优化

    SPFA是可以优化的,这个大家都是晓得的吧. 下面介绍两种SPFA的神奇优化(我只代码实现了的一种) SLF:Small Label First策略,设要加入的节点是j,队首元素为i,若dist(j) ...