数根

(又称数字根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. 优动漫PAINT-简单的树、叶教学

    如题,简单.好用:其实说的还是一个观察的事.看你是否足够细心,对于树叶的生长.枝桠和树干的关系是否了解咯. 对于这样的树枝丫和叶子完全可以使用优动漫PAINT完成,简单又快捷,软件下载:www.don ...

  2. vue项目测试和打包发布

    在线测试:npm run dev 发布打包:npm run build  打包后,代码文件在dist文件夹下面,可以正式发布了,复制到其它web服务器下面,在浏览器用http访问.

  3. 【转载】02-PowerDesigner的下载及安装

    原创路径:https://blog.csdn.net/ruyu00/article/details/79842807 一.下载 下载路径:https://pan.baidu.com/s/1WD7QHT ...

  4. [LeetCode] 155. minStack 设计最小栈

    注意:getMin()时间复杂度为O(1) 最原始的方法: class MinStack(object): def __init__(self): """ initial ...

  5. 魔兽争霸RPG游戏-军团战争-游戏经验总结

    终于要写这篇了,上一篇是个意外. 2015年关注,一代鬼王Xun和GGL比赛.晚上11点之后,经常有水友赛.主播xun,会带着一帮小弟,玩一些游戏.比如魔兽争霸6v6,2v2,RPG游戏-军团战争,疯 ...

  6. UVALive 3989 Ladies' Choice

    Ladies' Choice Time Limit: 6000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...

  7. 2016 10 27 考试 dp 向量 乱搞

    目录 20161027考试 T1: T2: T3: 20161027考试 考试时间 7:50 AM to 11:15 AM 题目 考试包 据说这是一套比较正常的考卷,,,嗯,,或许吧, 而且,,整个小 ...

  8. 6、python中的字符串

    最早的编码为ascii码,共256个符号.UTF-8是国际通用编码,全面支持中文,以一个字节表示英文,以三个字节表示一个中文以及其他语言:GB2312是我国自己定制的中文编码标准,使用1个字节表示英文 ...

  9. angularjs $http 服务

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  10. bzoj3931: [CQOI2015]网络吞吐量(spfa+网络流)

    3931: [CQOI2015]网络吞吐量 题目:传送门 题解: 现在有点难受....跳了一个多钟...菜啊... 题意都把做法一起给了....最短路+网路流啊. 不想说话...记得开long lon ...