树根 Digital root
数根
(又称数字根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 = 111 + 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的更多相关文章
- Digital root(数根)
关于digital root可以参考维基百科,这里给出基本定义和性质. 一.定义 数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这 ...
- 数字根(digital root)
来源:LeetCode 258 Add Dights Question:Given a non-negative integer num , repeatedly add all its digi ...
- 【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+..+ ...
- Sum of Digits / Digital Root
Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...
- digital root问题
问题阐述会是这样的: Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 1. 数字根(Digital Root)
数字根(Digital Root)就是把一个自然数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这个一位数便是原来数字的数字根.例如: 198的数字根为9(1+9+8=18,1 ...
- 快速切题 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 ...
- Codeforces Beta Round #10 C. Digital Root 数学
C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...
- 数学 - SGU 118. Digital Root
Digital Root Problem's Link Mean: 定义f(n)为n各位数字之和,如果n是各位数,则n个数根是f(n),否则为f(n)的数根. 现在给出n个Ai,求出A1*A2*…*A ...
随机推荐
- 好久不见我又回来了cnblogs
最近一直没时间写博客.... 最近工作中遇到的问题,当时花费了一定功夫才解决. 当再次遇到,同样问题的时候,我知道自己能够解决的,但是却同样,要走原来的弯路解决他. 说到底还时确实总结啊.大概是没时间 ...
- 使用rman恢复数据小结
恢复前提有数据备份 以 alter database open resetlogs 开机以后多要做一次全备(以前的备份失效了) 恢复参数文件: restore spfile from '/home/o ...
- Map和WeakMap的区别
个人总结:在一个变量作用域中,如果结束到作用域结尾 } 的话,map中的引用会被垃圾回收机制回收的是weakmap ,map中的引用不会被垃圾回收机制回收的是map. 强引用:只要引用存在,垃圾回收器 ...
- vue+element-ui+slot-scope或原生实现可编辑表格(日历)
你们公司的产品是不是还在做一个可编辑表格功能? 1.前言 咱开发拿到需求大多数是去网上找成型的组件,找不到再看原生的方法能否实现,大牛除外哈,大牛一般喜欢封装组件框架. 2.思路 可编辑表格在后台管理 ...
- C语言中数据类型的字节数
类型 16位 32 位 64位 char 1 1 1 short int 2 2 2 int 2 4 4 unsigned int 2 4 4 float 4 4 4 double 8 8 8 lon ...
- 嵌入式(C)笔试题
1 读程序段,回答问题 (a) int main(int argc,char *argv[]) { int c=9,d=0; c=c++%5; d=c; printf("d=%d\n&quo ...
- 【codeforces 67A】Partial Teacher
[题目链接]:http://codeforces.com/problemset/problem/67/A [题意] 给一个长度为n-1的字符串; 每个字符串是'L','R','='这3种字符中的一个; ...
- linux文件时间的查看和改动touch
1. linux文件的时间 linux下文件时间主要有以下三种: 1.1 modification time(mtime) 文件改动时间.即文件内容的改动时,更新这个时间.不包含文件权限和属性的改动. ...
- zoj 3820 Building Fire Stations (二分+树的直径)
Building Fire Stations Time Limit: 5 Seconds Memory Limit: 131072 KB Special Judge Marjar ...
- 由老同事学习SAP所想到的
前段时间一位老同事在微信上跟我说他们公司正计划导SAP系统,但整个IT中心几乎无人使用过SAP,知道我在这行业干了多年了,所以想问我怎么开始学习.于是我约他今天出来聊聊,顺便把手里的SAP ECC E ...