Problem Description
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.
Input
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.
Output
For each integer in the input, output its digital root on a separate line of the output.
Sample Input
24
39
0
Sample Output
6
3
一个数对九取余,得到的数称之为九余数;
一个数的九余数等于它的各个数位上的数之和的九余数!

题目大意:

给定一个正整数,根据一定的规则求出该数的“数根”,其规则如下:

例如给定 数字 24,将24的各个位上的数字“分离”,分别得到数字 2 和 4,而2+4=6;

因为 6 < 10,所以就认为6是数字24的“数根”;

而对于数字 39 , 将39的各个位上的数字“分离”,分别得到数字 3 和 9,而3+9=12,且12>10;

所以依据规则再对 12 进行相应的运算,最后得到数字3,而3<10,所以就认为3是数字39的“数根”。

通过运算可以发现任何一个数的“数根”都是一个取值范围在 1 ~ 9之间的正整数,

且任何一个正整数都只有唯一的一个“数根”与其相对应。

题目要求数字 n^n 的“数根”

解题思路:

九余数定理

一个数对九取余后的结果称为九余数。

一个数的各位数字之和想加后得到的<10的数字称为这个数的九余数(如果相加结果大于9,则继续各位相加)

代码如下:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char a[1010];
int i,j,s,l;
while(~scanf("%s",&a)&&a[0]!='0')
{
l=strlen(a);
s=0;
for(i=0;i<l;i++)
{
s=s+a[i]-'0';
}
s=s%9;
if(s==0)
s=9;
printf("%d\n",s);
}
return 0;
}

一个数对九取余,得到的数称之为九余数;
一个数的九余数等于它的各个数位上的数之和的九余数!

HDOJ 1013题Digital Roots 大数,9余数定理的更多相关文章

  1. HDOJ 1163 Eddy's digital Roots(九余数定理的应用)

    Problem Description The digital root of a positive integer is found by summing the digits of the int ...

  2. Eddy's digital Roots(九余数定理)

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  3. HDU-1163 Eddy's digital Roots(九余数定理)

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  4. HDU——1163Eddy's digital Roots(九余数定理+同余定理)

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  5. hdu 1163 Eddy's digital Roots 【九余数定理】

    http://acm.hdu.edu.cn/showproblem.php?pid=1163 九余数定理: 如果一个数的各个数位上的数字之和能被9整除,那么这个数能被9整除:如果一个数各个数位上的数字 ...

  6. 1013:Digital Roots

    注意:大数要用字符串表示! sprintf:字符串格式化命令 主要功能:将格式化的数据写入某个字符串缓冲区 头文件:<stdio.h> 原型 int sprintf( char *buff ...

  7. HDU 1013 Digital Roots(字符串,大数,九余数定理)

    Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. HDU 1013 Digital Roots【字符串,水】

    Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. HDU 1013 Digital Roots(to_string的具体运用)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1013 Digital Roots Time Limit: 2000/1000 MS (Java/Othe ...

随机推荐

  1. [字符串] AppMessage--字符串返回帮助类 (转载)

    点击下载 AppMessage.rar 这个类是关于AppMessage的帮助类,主要是格式化返回字符串的里面会有很多的提供字符的格式以及预设置内容,大家可以直接使用的看下面代码吧 /// <s ...

  2. Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException

    异常Log: Error:Execution failed for task ‘:app:dexDebug’. > com.android.ide.common.process.ProcessE ...

  3. DNS(域名系统)域名解析设置

    DNS(Domain Name System,域名系统), 因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串.通过主机名,最 ...

  4. SQL,学习基础2

    列=字段, 记录=实体  事物日志文件(用来记录数据库的增删情况,扩展名LDF) 数据库文件(但是只有一个是主数据库文件(即用它来启动的),其余为次数据库文件)mdf 数据类型: 整形(整数)——in ...

  5. 理解java Web项目中的路径问题

    本文以项目部署在tomcat服务器为例,其他相信也是一样的. 先说明请求页面的写法,在web中,页面路径主要写的有以下几种 1.请求重定向 2.浏览器的请求被服务器请求到新页面(我称为“转发”) 3. ...

  6. HDU 3006 The Number of set(位运算 状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3006 题目大意:给定n个集合,每个集合都是由大于等于1小于等于m的数字组成,m最大为14.由给出的集合 ...

  7. 【Java多线程与并发库】4.传统线程同步通信技术

    我们先通过一道面试题来了解传统的线程同步通信. 题目:子线程循环10次,接着主线程循环100次,接着又回到子线程循环10次,接着再回到主线程又循环100次,如此循环50次,请写出程序. 我没有看答案, ...

  8. Linux平台上搭建apache+tomcat负载均衡集群

    传统的Java Web项目是通过tomcat来运行和发布的.但在实际的企业应用环境中,采用单一的tomcat来维持项目的运行是不现实的.tomcat 处理能力低,效率低,承受并发小(1000左右).当 ...

  9. RecordSet .CacheSize, Properties,CurserType,PageSize

    使用 CacheSize 属性可以控制一次要从提供者那里将多少个记录检索到本地内存中.例如,如果 CacheSize 为 10,首次打开 Recordset 对象后,提供者将把前 10 个记录检索到本 ...

  10. Scroll view 备忘

    Stroyboard中使用ScrollView 当我们使用Storyboard开发项目时,如果要往控制器上拖入一个ScrollView并且添加约束设置滚动区域,是有特殊的规定的: 拖入一个scroll ...