LeetCode:Add Digits - 非负整数各位相加
2016-03-04 18:55
281 查看
1、题目名称
Add Digits (非负整数各位相加)
2、题目地址
https://leetcode.com/problems/add-digits/
3、题目内容
英文:Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
中文:有一个非负整数num,重复这样的操作:对该数字的各位数字求和,对这个和的各位数字再求和……直到最后得到一个仅1位的数字(即小于10的数字)。
例如:num=38,3+8=11,1+1=2。因为2小于10,因此返回2。
4、解题方法
Add Digits (非负整数各位相加)
2、题目地址
https://leetcode.com/problems/add-digits/
3、题目内容
英文:Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
中文:有一个非负整数num,重复这样的操作:对该数字的各位数字求和,对这个和的各位数字再求和……直到最后得到一个仅1位的数字(即小于10的数字)。
例如:num=38,3+8=11,1+1=2。因为2小于10,因此返回2。
4、解题方法
class Solution { public: int addDigits(int num) { if(num/10==0) return num; int middle=0; while(num>=10){ middle=middle+num%10; num=num/10; } return addDigits(middle+num); } };
相关文章推荐
- [bzoj4025]二分图
- Linux chown 命令
- PAT (Advanced Level) Practise 1017 Queueing at Bank (25)
- shiro简单配置(转)
- 【VS开发】C语言遍历文件夹
- 在windows上部署使用Redis
- 一个球从100m高度自由落下,每次落地后反弹回原来高度的一半,再落下。求它在第10次落地并反弹到最高点,共经过多少m?第10次反弹多高?
- 从setContentView方法分析Android加载布局流程
- Spring MVC不要在@Service bean中保存状态
- Intent和Intent过滤器
- cf557c
- 移动端开发需要注意的一些事情
- cookie与session
- 数据结构和算法经典书籍
- Python mysqldb: Library not loaded: libmysqlclient.18.dylib
- BZOJ 1210 HNOI 2004 邮递员 插头DP
- 1005. Spell It Right (20)
- sizeof('a')
- A Famous Music Compose
- 【GDKOI2016】魔卡少女Code&&Details