数字根(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 ...
随机推荐
- GridView自定义之前后效果对比
- highcharts 的使用实例:待写
http://www.hcharts.cn/demo/index.php 方法一:在Axis(包括xAxis和yAxis)有一个属性tickInterval,number类型,表示间隔,也就是间隔多少 ...
- 【题解】【BST】【Leetcode】Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路: ...
- linux性能监控基础命令
压力测试监控下系统性能方法之一 #top 该命令监控的是进程的信息 看图逐行意义 top:执行命令的之间 up:已经执行了277天 2users:目前有两个使用者,使用#who可以查看具体的使用者详情 ...
- Redis的初步安装
Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 下载 官网下载:http://redis.io/downlo ...
- 基于Spring框架的Web应用开发笔记 - Outline
Motivation 最近的工作涉及Web框架搭建,在了解公司原有采用框架基础上对Web开发技术栈做了一次升级,在次做记录. Audience J2EE Web Application Develop ...
- ME23N PO 打印预览 打印问题
ME23N进入PO订单,点message查看订单打印的配置 点message查看订单打印的配置 然后spro查看 选择相应的outputtype,然后双击processing routines 可以看 ...
- soapUI 在多个测试套件 testsuite 里,多个testcase里传值如何实现
1.首先 要添加一个全局 自定义变量 Custom Properties 2.用transfer property 将取来的值 放入到变量 getToken 里 3.在另一个testc ...
- 笨小猴 2008年NOIP全国联赛提高组
题目描述 Description 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大! 这种方法的具体描述如下:假设m ...
- spring beans源码解读之 ioc容器之始祖--DefaultListableBeanFactory
spring Ioc容器的实现,从根源上是beanfactory,但真正可以作为一个可以独立使用的ioc容器还是DefaultListableBeanFactory,因此可以这么说, DefaultL ...