九度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 ...
随机推荐
- 【POJ1276】Cash Machine(多重背包单调队列优化)
大神博客转载http://www.cppblog.com/MatoNo1/archive/2011/07/05/150231.aspx多重背包的单调队列初中就知道了但一直没(不会)写二进制优化初中就写 ...
- 【MFC】定时器的使用(转)
原文转自 http://blog.csdn.net/hellozhd/article/details/8213359 在对话框中使用定时器: 1.定义定时器 #define TIMER1 1 2.开启 ...
- Android 4.4 不休眠+不锁屏+默认中文+去除导航栏
1.不休眠 frameworks/base/packages/SettingsProvider/res/values/defaults.xml 里面60000改成-1,就是不进入休眠. 这个文件还保存 ...
- CentOS找不到想要的镜像版本?
CentOS找不到想要的镜像版本? 情景: 当学习Linux时,一般教程不是最新的,教程里的CentOS版本也就不是最新的,这个时候, 在看着教程练习的时候就需要安装指定的版本,避免因为版本不同造成困 ...
- datetimepicker[jquery-ui]时间控件的三种初始化方法
1.只显示年月日 $( ".datepicker").datepicker({ needDay:true, changeMonth: true, //显示月份 changeYear ...
- 利用github搭建个人网站
1.注册一个github https://github.com/ 2.新建一个仓库 仓库名 用 Owner.github.io 的格式,然后点击创建 3.源码上传至github 安装github桌 ...
- OceanBase数据库实践入门——手动搭建OceanBase集群
前言 目前有关OceanBase功能.案例.故事的文章已经很多,对OceanBase感兴趣的朋友都想安装一个数据库试试.本文就是分享初学者如何手动搭建一个OceanBase集群.这也是学习理解Ocea ...
- Codeforces Gym - 101147J Whistle's New Car
Discription Statements Whistle has bought a new car, which has an infinite fuel tank capacity. He di ...
- Maven教程:tutorialspoint-maven
来自turorialspoint的Maven教程(英文),官网:http://www.tutorialspoint.com/maven/index.htm 这个教程在国内已经被翻译成中文,官网:htt ...
- array_map常用技巧
array_map() 函数将用户自定义函数作用到数组中的每个值上,并返回用户自定义函数作用后的带有新值的数组. 简单来说 “array_map” 会对数组中的每一项进行处理,并返回处理后的数据. 定 ...