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. 编译nginx错误:make[1]: *** [/pcre//Makefile] Error 127

    --with-pcre=DIR 是设置源码目录,而不是编译安装后的目录.

  2. HTML和CSS基础知识

    html基本结构<html>内容</html> html开始标记<head>内容</head> html文件头标记<title>内容< ...

  3. 为什么我们要做三份 Webpack 配置文件

    时至今日,Webpack 已经成为前端工程必备的基础工具之一,不仅被广泛用于前端工程发布前的打包,还在开发中担当本地前端资源服务器(assets server).模块热更新(hot module re ...

  4. 深入理解java虚拟机读后总结

    之前看过,很多会遗忘,标记一下,温故知新.(明天的我一定会感谢现在努力的自己. ) 一.运行时数据区域 Java虚拟机管理的内存包括几个运行时数据内存:方法区.虚拟机栈.本地方法栈.堆.程序计数器,其 ...

  5. c++高精度计算(加法)

    本文提供给刚入坑的新手 关于高精度的计算网上百度一下可以了解到许多 今天我分享的只是一些自己的心得,更详细的可以去看原博主的原创文章(https://blog.csdn.net/fanyun_01/a ...

  6. Python中级 —— 05访问数据库

    ** 写在前面 ------------------> ** 廖雪峰 菜鸟 数据库类别 首先选择一个关系数据库.目前广泛使用的关系数据库也就这么几种: 付费的商用数据库: Oracle:典型的高 ...

  7. flask中的蓝图实现模块化的应用

    Blueprint 蓝图的基本设想是当它们注册到应用上时,它们记录将会被执行的操作. 当分派请求和生成从一个端点到另一个的 URL 时,Flask 会关联蓝图中的视图函数. 简单来说,Blueprin ...

  8. PhpStorm 克隆下来的项目改动,版本控制不起效果

    打开的目录 —>多个 git项目—>版本控制无法监控. 打开的目录 —>一个 git项目—>版本控制可以监控.

  9. day 33 线程

    1.线程理论 什么是线程:线程是cpu的最小执行单位(实体),进程是操作系统的数据资源分配单位   2.线程的两种创建方式(重点) 查看线程的pid:使用os模块查看id,线程的id应该是相同的 3. ...

  10. 电竞大数据平台 FunData 的系统架构演进

      电竞大数据时代,数据对比赛的观赏性和专业性都起到了至关重要的作用.同样的,这也对电竞数据的丰富性与实时性提出了越来越高的要求. 电竞数据的丰富性从受众角度来看,可分为赛事.战队和玩家数据:从游戏角 ...