九度OJ 1124:Digital Roots(数根) (递归)
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:2963
解决:1066
- 题目描述:
-
The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process
is repeated. This is continued as long as necessary to obtain a single digit.
For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the
process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.
- 输入:
-
The input file will contain a list of positive integers, one per line.
The end of the input will be indicated by an integer value of zero.
- 输出:
-
For each integer in the input, output its digital root on a separate line of the output.
- 样例输入:
-
24
39
0
- 样例输出:
-
6
3
- 提示:
-
The integer may consist of a large number of digits.
思路:
递归,结束条件是仅剩一位数。主要考察数位的分解。
代码:
#include <stdio.h>
#include <string.h> #define M 1000000 int main(void)
{
long long a, b;
char s[M+1];
int i; while (scanf("%s", s) != EOF)
{
if (strcmp(s, "0") == 0)
break; a = 0;
for (i=0; s[i]; i++)
a += s[i]-'0'; while (a>=10)
{
b = 0;
while (a)
{
b += a%10;
a /= 10;
}
a = b;
} printf("%lld\n", a);
} return 0;
}
/**************************************************************
Problem: 1124
User: liangrx06
Language: C
Result: Accepted
Time:10 ms
Memory:1816 kb
****************************************************************/
九度OJ 1124:Digital Roots(数根) (递归)的更多相关文章
- 九度OJ 1124 Digital Roots -- 数位拆解
题目地址:http://ac.jobdu.com/problem.php?pid=1124 题目描述: The digital root of a positive integer is found ...
- 九度OJ 1402 特殊的数 -- 位操作
题目地址:http://ac.jobdu.com/problem.php?pid=1402 题目描述: 现在有n个数,其中有一些出现了一次,一些出现了两次,一些出现了很多次.现在要求你找出那些只出现一 ...
- 九度OJ 1209 最小邮票数 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1209 题目描述: 有若干张邮票,要求从中选取最少的邮票张数凑成一个给定的总值. 如,有1分,3分,3分,3 ...
- 九度OJ 1214 寻找丑数【算法】
题目地址:http://ac.jobdu.com/problem.php?pid=1214 题目描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因 ...
- 九度OJ 1183 守形数 (模拟)
题目1183:守形数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2663 解决:1424 题目描写叙述: 守形数是这样一种整数.它的平方的低位部分等于它本身. 比方25的平方是625. ...
- 九度OJ 1214:丑数 (整除)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2180 解决:942 题目描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因 ...
- 九度OJ 1060:完数VS盈数 (数字特性)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5590 解决:2093 题目描述: 一个数如果恰好等于它的各因子(该数本身除外)子和,如:6=3+2+1.则称其为"完数" ...
- 九度OJ 1050:完数 (数字特性)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7535 解决:3125 题目描述: 求1-n内的完数,所谓的完数是这样的数,它的所有因子相加等于它自身,比如6有3个因子1,2,3,1+2+ ...
- 九度OJ 1129:Skew数 (大数运算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:734 解决:548 题目描述: 在 skew binary表示中, 第 k 位的值xk表示xk*(2k+1-1). 每个位上的可能数字是0 ...
随机推荐
- 如果dom节点是动态添加进页面的,在页面节点绑定事件如何解决的问题。
如果dom节点是动态添加进页面,想在节点绑定事件,传统的做法就是遍历节点,但会出现问题,也肯能有其他的办法,突然想到 可以依据事件冒泡,这样就不惧页面后添加节点而不响应事件的问题.比较结实.示例代码如 ...
- vue.js源码学习分享(二)
/** * Check if value is primitive//检查该值是否是个原始值 */ function isPrimitive (value) { return typeof value ...
- kafka优化–JVM参数配置优化
https://blog.csdn.net/u013063153/article/details/73826403
- doT.js-doT模板方便快捷的组织页面DOM
重来没有想过,作为一个坐吃等死的前端也会有学习引擎模板的一天 都是被现实所逼呀.学习优秀代码时,一句一句翻译.忽然看到{{ }}这个包裹的代码.糟心了!看不懂,咋办?学呀!!!!!! 这是我开始学 ...
- R语言实战读书笔记(六)基本图形
#安装vcd包,数据集在vcd包中 library(vcd) counts <- table(Arthritis$Improved)counts # 垂直barplot(counts, main ...
- 【APIO2015】Bali Sculptures
题目描述 印尼巴厘岛的公路上有许多的雕塑,我们来关注它的一条主干道. 在这条主干道上一共有 $N$ 座雕塑,为方便起见,我们把这些雕塑从 $1$ 到 $N$ 连续地进行标号,其中第 $i$ 座雕塑的年 ...
- java布局(每个名字都是有意义的)
一.FlowLayout 1.流水布局:从左至右,排满换行 2.构造函数有三种: (1)FlowLayout() (2)FlowLayout(align) (3)FlowLayout(align, h ...
- Java 获取当前时间及实现时间倒计时功能
引言 在一些项目中或是一些特殊的业务场景中,需要用到显示系统的当前时间,以及一些固定的时间倒计时,时间到后做一些什么事情的业务 .接下来咱们就具体看看代码是怎么实现的: <%@ page lan ...
- MongoDb 判断字段长度比较好的方法
查询某字段长度超过一定长度时的方法, MongoDB中可能不好处理,一般这样: db.test.find({ $where:"this.F_DAQDATA.legnth>600&quo ...
- mac os安装jdk、卸载
1.JAVA版本8u171与8u172的区别 https://blog.csdn.net/u014653815/article/details/80435226 奇数版本是稳定版本,上面修订的所有 ...