九度OJ 1124 Digital Roots -- 数位拆解
题目地址:http://ac.jobdu.com/problem.php?pid=1124
- 题目描述:
-
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> void digital_roots (char data[]){
int ans;
int len;
int i; len = strlen (data);
ans = 0;
for (i=0; i<len; ++i)
ans += data[i] - '0';
i = 0;
while (ans){
data[i] = ans % 10 + '0';
ans /= 10;
++i;
}
data[i] = '\0';
} int main(void){
char input[10001]; while (scanf ("%s", input) != EOF && input[0] != '0'){
while (strlen (input) > 1){
digital_roots (input);
}
printf ("%d\n", input[0] - '0');
} return 0;
}
九度OJ 1124 Digital Roots -- 数位拆解的更多相关文章
- 九度OJ 1124:Digital Roots(数根) (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2963 解决:1066 题目描述: The digital root of a positive integer is found by s ...
- 【九度OJ】题目1124:Digital Roots 解题报告
[九度OJ]题目1124:Digital Roots 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1124 题目描述: T ...
- 【九度OJ】题目1015:还是A+B 解题报告
[九度OJ]题目1015:还是A+B 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1015 题目描述: 读入两个小于10000的正整 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ 1502 最大值最小化(JAVA)
题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...
- 九度OJ,题目1089:数字反转
题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...
- 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...
- 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...
- 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)
题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
随机推荐
- HDOJ-ACM1019(JAVA) 多个数的最小公倍数
题意:求多个数的最小公倍数 很简单,但是我一开始的做法,估计会让结果越界(超过int的最大值) import java.util.*; import java.io.*; public class M ...
- .Net设计模式_开篇
前言 其实以前看过两次设计模式,现在想来,几乎已经对设计模式没有任何印象,说明根本没有理解.或者说几乎不用,所以我除了单列.工厂外的设计模式几乎全部忘记了.最近需要写一个引擎,想用UML设计整体的架构 ...
- Apache CXF框架结构和基本原理(转)
原文链接:http://blog.sina.com.cn/s/blog_6182547f01017pak.html CXF旨在为服务创建必要的基础设施,它的整体架构主要由以下几个部分组成: 1.Bus ...
- 树莓派(Rospberry Pi B+)到货亲測
1 图鉴 Rospberry Pi B+最终在今天下午有蜗牛快递公司圆*送到了.B+主要是添加了2个USB,添加了GPIO,sd卡换成了micro sd ...先不说直接上图再说,期待了好久好久 w ...
- Android SimpleAdapter源码详解
一直没认真看过android的源码,也不太敢看,稀里糊涂也敲了一年的代码,现在想好好学习了,就把常用的源码都看了一下,小伙伴们来涨姿势吧,有错误的地方,直接指出,我脸厚不怕丢人.来吧. 刚开始学and ...
- Windows下FFmpeg高速入门
本系列文章导航 Windows下FFmpeg高速入门 ffmpeg參数解释 mencoder和ffmpeg參数具体解释(Java处理视频) Java 生成视频缩略图(ffmpeg) 使用ffmpeg进 ...
- 解决错误提示: 未找到 Oracle 客户端和网络组件。
问题描述: 环境:winxp运行程序连接远程Oracle数据库时报错:未找到 Oracle 客户端和网络组件.这些组件是由 Oracle 公司提供的,是 Oracle 8i 版 (或更高) 客户软件安 ...
- 机器学习算法-K-means聚类
引文: k均值算法是一种聚类算法.所谓聚类.他是一种无监督学习,将类似的对象归到同一个蔟中.蔟内的对象越类似,聚类的效果越好. 聚类和分类最大的不同在于.分类的目标事先已知.而聚类则不一样. 由于其产 ...
- android优化(json工具,message新建/传递,avtivity深入学习视频)
1,在线json校验工具:www.bejson.com 2, 在handler中经常使用的 message的传递上,message.what使用静态量 . private static final i ...
- 在其它路径新建cocos2d-x项目
打开vs2010程序,然后选择“文件—新建—项目”,如图 改了一下位置,放在D:\Program Files\cocos2d-x\ ,确定 然后点下一步 这个程序不需要物理引擎,所以可以把上面红色的圈 ...