博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Digital Roots
阅读量:6461 次
发布时间:2019-06-23

本文共 1316 字,大约阅读时间需要 4 分钟。

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

24390

Sample Output

63

Source

Greater New York 2000

#include <iostream>

#include<string>
using namespace std;
string a;
int main()
{
    while(cin>>a&&a!="0")
    {int n=0;
for(int a_index = 0; a_index < a.length(); a_index++)
    {
    n= (a[a_index] - '0')+n;
    if(n>9)
        n=n%10+n/10;}
    cout<<n<<endl;}
    return 0;
    }

转载于:https://www.cnblogs.com/lengxia/p/4387875.html

你可能感兴趣的文章
我们该怎么成为技术尖子生
查看>>
关于缓存使用中的一些看法
查看>>
Tpcc安装
查看>>
我的友情链接
查看>>
easyui 扩展loading
查看>>
第二章、Linux操作系统及常用命令
查看>>
面向对象编程:概述
查看>>
python温度转换
查看>>
matlab-自控原理 已知x~=Ax+Bu中的AB矩阵和X0,求单位输入下的时间响应
查看>>
23_Shell语言————位置变量($@、$、$#、shift)
查看>>
缓存架构设计
查看>>
【DBA之路】第1回 表空间的做成和扩张
查看>>
linux Apache2.4安装提示APR not found的解决办法
查看>>
CSS颜色代码(转载)
查看>>
mysql修改最大连接数笔记
查看>>
C#实现10进制转2进制
查看>>
js和jquery给iframe src赋值的3种方法
查看>>
python爬虫知识点总结(十六)PySpider框架概述和用法详解
查看>>
Structs 2 session 学习
查看>>
百度地图API-自定义图标覆盖物
查看>>