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. linux的pthread_self与gettid的返回值和开销的区别

    linux的pthread_self与gettid的返回值和开销的区别 linux的pthread_self与gettid的返回值和开销的区别 分类: 一些思考 2012-05-18 12:25 17 ...

  2. gluoncv 用已经训练好的模型参数,检测物体

    当然这个模型参数,最好用自己的,否则不够精确,我自己的还没训练完. from matplotlib import pyplot as plt import gluoncv from gluoncv i ...

  3. 【react】慕课网视频学习笔记

    1.JSX:语法糖,对语言的功能并没有影响,但更方便程序员使用,增强可读性. 2.jsFiddle:前端在线调试工具 3.为什么要把this额外声明成_self变量,因为window.setTimeo ...

  4. 2、Android-UI(关于Nine-Patch图片)

    实例: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...

  5. 《metasploit渗透测试魔鬼训练营》学习笔记第三章----情报搜集

    Kali渗透测试系统集成了metasploit开源的漏洞测试框架,是渗透测试必备的神器.下面是我在学习metasploit的笔记,有什么错误的地方请指出来,我会立即纠正的~ 一.情报搜集     1. ...

  6. 将本地已经存在的非git项目提交到github上的空仓库

    一.本地项目执行操作 1.在本地项目目录下初始化git仓库 git init 2.将本地项目下工作区的所有文件添加到git版本库的暂存区中 git add . (可以创建.gitignore文件忽略不 ...

  7. .net core RabbitMQ 消息队列

    上篇我们说到erlang的安装,现在有了基础前提,就可以继续安装RabbitMQ了! 这里我选用的RabbitMQ版本是: PS:这个RabbitMQ版本是要对应前面erlang版本,所以前面我们安装 ...

  8. 简单说明一下JS中的函数声明存在的“先使用,后定义”

    首先看一段JS代码,其中使用了两种方式声明了两个函数,分别在不同的地方调用两个函数: <script> 'use strict'; // 输出hello函数 console.log(hel ...

  9. 我的QT5学习之路(一)——浅谈QT的安装和配置

    一.前言 说到Qt,不能不说到C++,这门伟大的语言.因为其面向对象的编程思想和陡峭的学习曲线,一开始学习起来很是吃力.Qt从QT4开始基本封装了很多C++的工具库和界面库,而且支持跨平台,这是它最大 ...

  10. STM32F103 ucLinux开发之三(内核启动后不正常)(完结)

    STM32F103 ucLinux内核没有完全启动 从BOOT跳转到内核后,执行一长段的汇编语言,然后来到startkernel函数,开启C语言之旅. 但是内核输出不正常,如下所示: Linux ve ...