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
--------------------------------------------------------------------------------------------------------
一个数和它各位数的和同模。
证明:

首先n=d1+10d2+…+10m-1dm。则n= 9d2+…+(10m-1-1)dm+ d1+d2+…+dm,把所有的位数相加结果就是9的倍数取余,余数为n’=d1+d2+…+dm,所以n与n’同模。最终,经过不断取余,n会化为个位数。

${\rm{x}} = \sum\limits_{i = 1}^n {{d_i}{{10}^i}} $

${10^i} \equiv {1^i} \equiv 1$

$x = \sum\limits_{i = 1}^n {{d_i}(\bmod 9)} $

设${\rm{x}} = \sum\limits_{i = 1}^n {{d_i}} $

f(x)=x(mod 9)

f(f(x)) = f(x) = x(mod 9)

完整的公式为

${\rm{digit\_root(n) = }}\left\{ \begin{array}{l}
0,if(n = 0)\\
9,if(n \ne 0,n \equiv 0\bmod 9)\\
n\bmod 9,if(n \ne 0\bmod 9)
\end{array} \right.$

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define SIZE 100000
char n[SIZE];
int main()
{
int ans, length;
while (scanf("%s", n)== 1 && n[0] != '0')
{
ans = 0;
length = strlen(n);
for (int i = 0; i < length; i++)
{
ans += n[i] - '0';
} printf("%d\n", ans%9==0?9:ans%9);
}
return 0;
}

  

ACM1013:Digital Roots的更多相关文章

  1. 【九度OJ】题目1124:Digital Roots 解题报告

    [九度OJ]题目1124:Digital Roots 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1124 题目描述: T ...

  2. POJ 1519:Digital Roots

    Digital Roots Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25766   Accepted: 8621 De ...

  3. 九度OJ 1124:Digital Roots(数根) (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2963 解决:1066 题目描述: The digital root of a positive integer is found by s ...

  4. 1013:Digital Roots

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

  5. HDU - 1310 - Digital Roots

    先上题目: Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  6. Digital Roots 分类: HDU 2015-06-19 22:56 13人阅读 评论(0) 收藏

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

  7. Digital Roots:高精度

    C - Digital Roots Description The digital root of a positive integer is found by summing the digits ...

  8. 解题报告:hdu1013 Digital Roots

    2017-09-07 22:02:01 writer:pprp 简单的水题,但是需要对最初的部分进行处理,防止溢出 /* @theme: hdu 1013 Digital roots @writer: ...

  9. Eddy's digital Roots

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

随机推荐

  1. 关于hashcode 里面 使用31 系数的问题

    首先我们来了解一下hashcode,什么是hashcode?有什么作用? hashcode其实就是散列码,使用hashcode使用高效率的哈希算法来定位查找对象! 我们在使用容器来存储数据的时候会计算 ...

  2. 用POP动画模拟真实秒钟摆动效果

    用POP动画模拟真实秒钟摆动效果 静态图: 动画图: 此处用到了POP中的Spring系列动画,现提供源码如下: SecondClockView.h 与 SecondClockView.m // // ...

  3. exchange 删除邮件

    一 批量删除特定主题的邮件1.1 批量删除所有数据库中特定主题的邮件1) 群发了几封主题为“backup”的邮件: 2) 当前操作账号需要满足如下需求: a)该账号需属于Exchange Server ...

  4. 使用TryUpdateModel进行数据更新

    在控制器中可以使用TryUpdateModel或者UpdateModel方法来对指定的数据Model进行更新,如图所示的更新操作. POST请求数据如下所示 使用如下代码就可以对指定的字段进行更新 使 ...

  5. 乘风破浪:LeetCode真题_005_Longest Palindromic Substring

    乘风破浪:LeetCode真题_005_Longest Palindromic Substring 一.前言 前面我们已经提到过了一些解题方法,比如递推,逻辑推理,递归等等,其实这些都可以用到动态规划 ...

  6. 沉淀再出发:IoC和AOP的本质

    沉淀再出发:IoC和AOP的本质 一.前言 关于IoC和AOP这两个概念,如果我们没有深入的理解可以说是根本就不理解Spring这个架构的,同样的由Spring演变出来的Spring Boot和Spr ...

  7. August 27th 2017 Week 35th Sunday

    You can't be brave if you've only had wonderful things happen to you. 人生若只是有美好的境遇,那你也没办法学会勇敢. Wherea ...

  8. Mac远程连接服务器

    方法一:ssh 方法二:command+K进入远程桌面,这种方式类似于windwos下的远程桌面

  9. shell基础学习

    1. #! /bin/bash #设置只读变量,只读变量不可修改myUrl="http://www.baidu.com"readonly myUrl #删除变量,unset不能删除 ...

  10. Spark SQL on Yarn-Cluster

    Spark SQL 或者Hive SQL在yarn-client模式下运行正常,但是在yarn-cluster模式下总是报 status failed错误, 需要把$SPARK_HOME/lib/sp ...