时间限制: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.

来源:
2008年北京大学方正实验室计算机研究生机试真题

思路:

递归,结束条件是仅剩一位数。主要考察数位的分解。

代码:

#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(数根) (递归)的更多相关文章

  1. 九度OJ 1124 Digital Roots -- 数位拆解

    题目地址:http://ac.jobdu.com/problem.php?pid=1124 题目描述: The digital root of a positive integer is found ...

  2. 九度OJ 1402 特殊的数 -- 位操作

    题目地址:http://ac.jobdu.com/problem.php?pid=1402 题目描述: 现在有n个数,其中有一些出现了一次,一些出现了两次,一些出现了很多次.现在要求你找出那些只出现一 ...

  3. 九度OJ 1209 最小邮票数 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1209 题目描述: 有若干张邮票,要求从中选取最少的邮票张数凑成一个给定的总值.     如,有1分,3分,3分,3 ...

  4. 九度OJ 1214 寻找丑数【算法】

    题目地址:http://ac.jobdu.com/problem.php?pid=1214 题目描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因 ...

  5. 九度OJ 1183 守形数 (模拟)

    题目1183:守形数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2663 解决:1424 题目描写叙述: 守形数是这样一种整数.它的平方的低位部分等于它本身. 比方25的平方是625. ...

  6. 九度OJ 1214:丑数 (整除)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2180 解决:942 题目描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因 ...

  7. 九度OJ 1060:完数VS盈数 (数字特性)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5590 解决:2093 题目描述: 一个数如果恰好等于它的各因子(该数本身除外)子和,如:6=3+2+1.则称其为"完数" ...

  8. 九度OJ 1050:完数 (数字特性)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7535 解决:3125 题目描述: 求1-n内的完数,所谓的完数是这样的数,它的所有因子相加等于它自身,比如6有3个因子1,2,3,1+2+ ...

  9. 九度OJ 1129:Skew数 (大数运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:734 解决:548 题目描述: 在 skew binary表示中, 第 k 位的值xk表示xk*(2k+1-1).  每个位上的可能数字是0 ...

随机推荐

  1. net1:post,get方式传值,读写cookie,读XML文件,写script语句,跳转页面,response与request类

    原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  2. C语言中的bool类型

    C99中提供了一个头文件 <stdbool.h> 定义了bool代表_Bool,true代表1,false代表0.只要导入 stdbool.h ,就能非常方便的操作布尔类型了. 代码如下: ...

  3. LeetCode OJ--Permutation Sequence *

    求第k个排列. 刚开始按照一个排列一个排列的求,超时. 于是演算了一下,发下有数学规律,其实就是康托解码. 康托展开:全排列到一个自然数的双射 X=an*(n-1)!+an-1*(n-2)!+...+ ...

  4. Codeforces 149D Coloring Brackets(树型DP)

    题目链接 Coloring Brackets 考虑树型DP.(我参考了Q巨的代码还是略不理解……) 首先在序列的最外面加一对括号.预处理出DFS树. 每个点有9中状态.假设0位不涂色,1为涂红色,2为 ...

  5. SSH命令总结

    目录 一.ssh命令 二.端口转发 三.scp 命令 四.rsync命令 五.sz和rz命令 六. ssh-agent 七.ssh执行命令不退出问题 参考文章 一.ssh命令 登录类型 密码登录: 服 ...

  6. Truck History(最小生成树)

    poj——Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27703   Accepted: 10 ...

  7. Jenkins自动化部署入门(一)

    开始使用 Jenkins 这一段时间,技术总监为了减少测试环境每次提交新增接口都要部署项目的时间,搞了一个jenkins持续集成github.docker,这样只要每次push代码都会自动部署,确实节 ...

  8. MongoDb 判断字段长度比较好的方法

    查询某字段长度超过一定长度时的方法, MongoDB中可能不好处理,一般这样: db.test.find({ $where:"this.F_DAQDATA.legnth>600&quo ...

  9. iOS -- YYText富文本

    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString: [NSString strin ...

  10. win7 32位配置apache+wsgi+django环境

    1下载xampp,里面有apache,mysql,phpmyadmin, 2 下载wsgi,http://download.csdn.net/download/copter/9192361 将对应的模 ...