九度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 ...
随机推荐
- ajax 分页(jquery分页插件pagination) 小例2
封装成:myPagination.js// ajax分页 function sendAjax(flag, dataParam, url, callback) {//封装的ajax: var shus ...
- 使用 IntelliJ IDEA 开发 Android 应用程序时配置 Allatori 进行代码混淆
IntelliJ IDEA 提供了非常强大的 Android 开发支持,就连 Google 官方推荐的 Android Studio 其实也是 IntelliJ IDEA 的一个 Android 开发 ...
- CUDA程序计时
之前写的CUDA程序,想测量一下性能,网上很多用的是CPU端计时,很不准确.翻了一下书,发现这里应该使用事件来计时. CUDA中的事件本质上是一个GPU时间戳,这个时间戳是在用户指定的时间点上记录的. ...
- java 正则表达式-忽略大小写与多行匹配
Pattern.compile("'.*?'", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
- linux sed 替换(整行替换,部分替换)、删除delete、新增add、选取
sed命令行格式为: sed [-nefri] ‘command’ 输入文本 常用选项: -n∶使用安静(silent)模式.在一般 sed 的用法中,所有来自 STDI ...
- 2016-2017 ACM-ICPC, South Pacific Regional Contest (SPPC 16)
题目链接 Codeforces_Gym_101177 Problem A Anticlockwise Motion 直接模拟即可 #include<iostream> #include ...
- HTTP基础认证Basic Authentication
HTTP基础认证Basic Authentication Basic Authentication是一种HTTP访问控制方式,用于限制对网站资源的访问.这种方式不需要Cookie和Session,只需 ...
- Ampzz 2011 Cross Spider 计算几何
原题链接:http://codeforces.com/gym/100523/attachments/download/2798/20142015-ct-s02e07-codeforces-traini ...
- debug : 调试主进程启动的子进程
http://blog.csdn.net/lostspeed/article/details/10109867
- JavaScript this用法总结(**************************************)
JavaScript this用法总结 在JavaScript中,this关键字可以说是最复杂的机制之一.对this的作用机制缺乏比较深入的理解很容易在实际开发中出现问题. 1.this的作用 为什么 ...