将一个数的各位数字加起来求和
2015-12-19 00:00
375 查看
摘要: 将一个数的各位数字加起来求和
一段十分简单的代码的背后常常对应的是一个完备的算法,否则代码不仅会显得臃肿和繁琐,而且非常容易出错。
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?
我们的代码如果只是按照传统的循环和递归方式去做的话,不仅增加算法的复杂度,而且也容易出错。为了提高运行的效率,我们需要先研究下这背后所蕴含的算法逻辑。
For example, the digital root of 65,536 is 7, because 6 + 5 + 5 + 3 + 6 = 25 and 2 + 5 = 7.
Digital roots can be calculated with congruences in modular arithmetic rather than by adding up all the digits, a procedure that can save time in the case of very large numbers.
Digital roots can be used as a sort of checksum. For example, since the digital root of a sum is always equal to the digital root of the sum of the summands' digital roots.[needs copy edit] A person adding long columns of large numbers will often find it reassuring to apply casting out nines to his result—knowing that this technique will catch the majority of errors.
Digital roots are used in Western numerology, but certain numbers deemed to have occult significance (such as 11 and 22) are not always completely reduced to a single digit.
The number of times the digits must be summed to reach the digital sum is called a number's additive persistence; in the above example, the additive persistence of 65,536 is 2.
It helps to see the digital root of a positive integer as the position it holds with respect to the largest multiple of 9 less than it. For example, the digital root of 11 is 2, which means that 11 is the second number after 9. Likewise, the digital root of 2035 is 1, which means that 2035 − 1 is a multiple of 9. If a number produces a digital root of exactly 9, then the number is a multiple of 9.
代码实现
搞清楚这套逻辑之后,即可以简单的代码进行实现了。而不必真的将一个数的每一位进行相加的操作了。
一段十分简单的代码的背后常常对应的是一个完备的算法,否则代码不仅会显得臃肿和繁琐,而且非常容易出错。
功能需求
首先看看这段代码需要实现怎样的功能: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?
我们的代码如果只是按照传统的循环和递归方式去做的话,不仅增加算法的复杂度,而且也容易出错。为了提高运行的效率,我们需要先研究下这背后所蕴含的算法逻辑。
算法逻辑
The digital root (also repeated digital sum) of a non-negative integer is the (single digit) value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached.For example, the digital root of 65,536 is 7, because 6 + 5 + 5 + 3 + 6 = 25 and 2 + 5 = 7.
Digital roots can be calculated with congruences in modular arithmetic rather than by adding up all the digits, a procedure that can save time in the case of very large numbers.
Digital roots can be used as a sort of checksum. For example, since the digital root of a sum is always equal to the digital root of the sum of the summands' digital roots.[needs copy edit] A person adding long columns of large numbers will often find it reassuring to apply casting out nines to his result—knowing that this technique will catch the majority of errors.
Digital roots are used in Western numerology, but certain numbers deemed to have occult significance (such as 11 and 22) are not always completely reduced to a single digit.
The number of times the digits must be summed to reach the digital sum is called a number's additive persistence; in the above example, the additive persistence of 65,536 is 2.
It helps to see the digital root of a positive integer as the position it holds with respect to the largest multiple of 9 less than it. For example, the digital root of 11 is 2, which means that 11 is the second number after 9. Likewise, the digital root of 2035 is 1, which means that 2035 − 1 is a multiple of 9. If a number produces a digital root of exactly 9, then the number is a multiple of 9.
代码实现
搞清楚这套逻辑之后,即可以简单的代码进行实现了。而不必真的将一个数的每一位进行相加的操作了。class Solution { public: int addDigits(int num) { return num - ((num -1)/9*9); } };
相关文章推荐
- 【讲古堂】浮点数
- MySQL-Binlog解析篇:4 大体框架出炉
- centos7配置JDK环境变量
- java实现泛型顺序栈
- java实现泛型队列
- java实现循环队列(顺序队列)
- Oracle MERGE raise ORA-00904 无效的标识符
- SercureCRT 连不上Linux 和配置SSH方法
- Xshell配置ssh免密码登录-密钥公钥(Public key)与私钥(Private Key)登
- SecureCRT® 远程连接不在同一个局域网的主机
- Using a SecureCRT 链接远程主机
- SecureCRT远程连接主机
- Linux 每日一练 :让你的Linux登录提示 be special one!!PS1
- Linux 每日一练 :显示你当前bash 的进程PID:$$
- linux 每日一练习:what have happened?指令执行的怎么样了?成功或者失败?
- linux 每日一练习:父程序与子程序的概念
- linux 每日一练习:环境变量
- REST another WebService???
- 计算器--K & R C
- Python单例模式的4种实现方法