数字根(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 ...
随机推荐
- [图论]Floyd 算法小结
Floyd 算法小结 By Wine93 2013.11 1. Floyd算法简介 Floyd算法利用动态规划思想可以求出任意2点间的最短路径,时间复杂度为O(n^3),对于稠密图, 效率要高于执行 ...
- [转]A plain english introduction to cap theorem
Kaushik Sathupadi Programmer. Creator. Co-Founder. Dad. See all my projects and blogs → A plain engl ...
- PM 时钟机制
PM 时钟机制 10.1 Minix3 PM 时钟机制概述在 MINIX3 中,除了前面所讲到的 CLOCK 时钟,在 pm 中也是维持了一个时钟, 我们暂且不分析为啥要这么做,我就分析是怎么实现这个 ...
- 【avalon】offsetParent
offsetParent: function () { var offsetParent = this[0].offsetParent while (offsetParent && a ...
- dedecms 列表页调用自定义字段
在列表附加字段中添加自己定义的字段 如: lwulr调用:{dede:list addfields="lwurl" channelid="1"}[field:l ...
- Qt之QPropertyAnimation
简述 QPropertyAnimation类定义了Qt的属性动画. QPropertyAnimation以Qt属性做差值,作为属性值存储在QVariants中,该类继承自QVariantAnimati ...
- iOS学习笔记---oc语言第二天
实例变量与方法 一.实例变量的可见度 二.方法 oc中的方法分两种:类方法和实例方法 类方法:只能类使用 eg:+ (id)alloc 注:类方法中不能使用实例变量 实例方法:只能对象使用,eg:- ...
- shamir叠像术 分类: 图像处理 2015-07-08 16:50 17人阅读 评论(1) 收藏
K=imread('SHNU.bmp'); height=info.Height; width=info.Width; A=zeros(height,width); B=zeros(height,wi ...
- leetcode 143. Reorder List ----- java
Given a singly linked list L: L0→L1→-→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do thi ...
- leetcode 129. Sum Root to Leaf Numbers ----- java
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...