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

本文共 1419 字,大约阅读时间需要 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

 

24 39 0

Sample Output

 

6 3

 

题目简单~:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using
namespace
std;
 
int
main(
int
argc,
char
*argv[])
{
    
char
c;
    
int
sum = 0;
     
    
while
(c =
getchar
())
    
{
        
if
(c ==
'\n'
)
        
{
            
cout << sum << endl;
          
            
c =
getchar
();
            
if
(c ==
'0'
)
                
break
;
            
else
                
sum = c -
'0'
;
                 
            
continue
;
        
}  
         
        
sum += c -
'0'
;
         
        
if
(sum > 9)
        
{
            
sum = sum%10 + sum/10;
        
}
             
    
}
     
    
return
0;
}
 
c – '0' 表示对0的距离

转载地址:http://ywnka.baihongyu.com/

你可能感兴趣的文章
[后台系统模板]优质、整洁的基于Bootstrap 3& Bootstrap 4的响应式后台管理系统模板...
查看>>
利用七牛 qshell 和 Automator 打造快捷上传服务
查看>>
制作kubernetes1.8离线安装包有感,我们的劳动如何变现
查看>>
cookie介绍一个简单的效果传值
查看>>
Egg + Vue 服务端渲染工程化建设
查看>>
监听微信手Q的挂起事件(visibilitychange)
查看>>
怎样在vue项目下添加ESLint
查看>>
七进七出React高阶组件
查看>>
Nginx 配置 https相关问题
查看>>
TenpayPasswordCtrl
查看>>
Python-Tips
查看>>
Apache 强制全站https请求(配置ssl证书)
查看>>
laravel源码分析之--Application 实例化
查看>>
Node.js源码解析-启动-js部分
查看>>
DOM操作--你究竟知道多少
查看>>
react组件生命周期理解
查看>>
flex布局中父容器属性部分演示效果
查看>>
前段开发环境部署(1)--nvm(node版本管理器)
查看>>
Javascript数组的“字符串”索引 & for…in 和 for…of的区别
查看>>
mysql函数集
查看>>