数字根(digital root)
来源: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)的更多相关文章
- 如何证明一个数的数根(digital root)就是它对9的余数?
数根就是不断地求这个数的各位数之和,直到求到个位数为止.所以数根一定和该数模9同余,但是数根又是大于零小于10的,所以数根模9的余数就是它本身,也就是说该数模9之后余数就是数根. 证明: 假设有一个n ...
- 树根 Digital root
数根 (又称数字根Digital root)是自然数的一种性质.换句话说.每一个自然数都有一个数根.数根是将一正整数的各个位数相加(即横向相加),若加完后的值大于等于10的话,则继续将各位数进行横向相 ...
- 1. 数字根(Digital Root)
数字根(Digital Root)就是把一个自然数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这个一位数便是原来数字的数字根.例如: 198的数字根为9(1+9+8=18,1 ...
- Digital root(数根)
关于digital root可以参考维基百科,这里给出基本定义和性质. 一.定义 数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这 ...
- digital root问题
问题阐述会是这样的: Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 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 ...
- Digital Root 的推导
背景 在LeetCode上遇到这道题:Add Digits 大意是给一个数,把它各位数字相加得到一个数,如果这个数小于10就返回,不然继续 addDigits(这个相加得到的数). 题目很简单,但是如 ...
- codeforces 10C Digital Root(非原创)
Not long ago Billy came across such a problem, where there were given three natural numbers A, B and ...
随机推荐
- HDU 2829 Lawrence (斜率DP)
斜率DP 设dp[i][j]表示前i点,炸掉j条边的最小值.j<i dp[i][j]=min{dp[k][j-1]+cost[k+1][i]} 又由得出cost[1][i]=cost[1][k] ...
- 解决input之间的空隙
<!doctype html> <html> <head> <meta charset="UTF-8"> <meta name ...
- 四则运算<3>
//李妍 2015.3.12 //四则运算新 #include<iostream> #include<fstream> #include<iomanip> #inc ...
- 对dataTable去重
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.T ...
- web.config的数据库连接字符串进行加密
连接参考:http://wenku.baidu.com/link?url=nwGug8wxz143A4pvBE_kN6vMU7aF3ojwCKJOyN-TQleZ07iAYrjx_FnFVDOtZAF ...
- centos 用户组
centos 用户组和用户的添加及所属问题 指定附加组 eg:dy 为组名 ,lisi 为用户,要把lisi 用户添加到 dy 组里面 useradd -G dy lisi 可以同时添加多个组, ...
- javabean实现serializable有什么用?为什么数据库持久就Bean实现这个接口?
Java的"对象序列化"能让你将一个实现了Serializable接口的对象转换成一组byte,这样日后要用这个对象时候,你就能把这些byte数据恢复出来,并据此重新构建那个对象了 ...
- C语言中的三字母词
C语言中的三字母词(trigraph) 在ANSI C标准中,定义了9个三字母词(trigraph),三字母词就是几个字符的序列,合起来表示另一个字符.三字母词使C语言环境可以在缺少一些必需字符的字符 ...
- mysql修改root密码和设置权限
整理了以下四种在MySQL中修改root密码的方法,可能对大家有所帮助! 方法1: 用SET PASSWORD命令 mysql -u root mysql> SET PASSWORD FOR ' ...
- POJ 1523 SPF(寻找关节点)
SPF Time Limit: 1000MS Memory ...