C - Digital Roots

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,把n的每一位加起来,求出新的n来,不断地加,直到n小于10.用到九余定理。

#include<iostream>
#include<string>
using namespace std;
int main() {
string s;
while (cin>>s) {
int ans = 0;
int lens = s.size();
for (int i = 0; i < lens; i++) {
ans += s[i] - '0';
}
if (ans == 0)break;
else
cout << (ans- 1) % 9 + 1 << endl; //九余定理
}
return 0;
} /*
设x是一个四位数
x=a*1000+b*100+c*10+d => x=a*999+b*99+c*9+d+a+b+c =>要求a+b+c+d 即求 (a*999+b*99+c*9+d+a+b+c)%9即可, 即求x%9
特例x==9时,,所以先-1,取余9后再+1
例如45=4+5=9
如果9%9==0 但是结果是0
*/

Digital Roots:高精度的更多相关文章

  1. Digital Roots 1013

    Digital Roots 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:456            测试通过:162 描述 T ...

  2. Eddy's digital Roots

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

  3. 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 ...

  4. HDU 1163 Eddy's digital Roots

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

  5. ACM——Digital Roots

    http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1028 Digital Roots 时间 ...

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

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

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

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

  8. HDU1163 Eddy&#39;s digital Roots【九剩余定理】

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

  9. HDU 1013 Digital Roots(字符串)

    Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...

随机推荐

  1. Spring异步-@Async注解

    Spring异步:@Async注解 使用@Async前需要开启异步支持:@EnableAsync 注解和XML方式 @Async返回值的调用:需要使用Future包装 1.如果没有使用Future包装 ...

  2. ubuntu下USB口插入USB转TTL查看串口号

    首先先要获取权限 sudo su 然后 cd /devls ls可以列出所有的串口号(确保此时USB转TTL已经插在电脑上了) 然后拔掉USB转TTL 在ls一下列出所有的串口设备 对比可以发现,插上 ...

  3. Python 基础 函数

    python 什么是函数 Python不但能非常灵活地定义函数,而且本身内置了很多有用的函数,可以直接调用.   python 函数的调用 Python内置了很多有用的函数,我们可以直接调用. 要调用 ...

  4. vue 复习(2)v-bind的应用 v-bind:classv-binf:style

    dasdclass与style绑定v-bind 1. 绑定HTML Class 对象语法 有些时候我们想动态的切换class的类名.在原生的js或jq中我们就要通过事件来动态的改变class类名,但在 ...

  5. js基础(闭包)

    1. "闭包就是跨作用域访问变量." [示例一] var name = 'wangxi' function user () { // var name = 'wangxi' fun ...

  6. py基础---多线程、多进程、协程

    目录 Python基础__线程.进程.协程 1.什么是线程(thread)? 2.什么是进程(process)? 3.进程和线程的区别 4.GIL全局解释器锁 5.多线程(threading模块) 6 ...

  7. Docker 学习:制作一个dockerfile

    dockerfile, 主要是四部分组成:基础镜像信息.维护者信息.镜像操作指令.容器启动执行指令. step 1: 按照语法,如下写一个centos操作系统的nignx镜像. 然后记得:wq保存和退 ...

  8. echarts踩坑---容器高度自适应

    在echarts中,若设置固定高度,当柱状图数据过多时会出现数据相互挤压,给用户的体验十分糟糕,可以通过给容器设置自适应高度,然后通过getDom()的方法解决此问题. 具体解决办法如下: <d ...

  9. Oracle之plsql及游标

    --1.赋值 --:= 赋值 declare var_name ) :='&请输入名字';--&是一个提示输入的特殊符号,会打开一个输入框 var_age ) :='&请输入年 ...

  10. python 文件读取方法详解

    话不多说直接码 # 绝对路径 # f = open('/Users/fangxiang/Downloads/我的古诗.text', mode='r', encoding='utf-8') # cont ...