hduoj1013,Digital Roots,又见数论,后悔高中没好好学奥数了
来源:互联网 发布:风行网络电视播放器 编辑:程序博客网 时间:2024/04/29 21:06
Digital Roots
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
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
分析:
一个数的根(姑且这么叫吧),即为所有位上的数之和减一对9取余加一。
code:
#include<iostream>using namespace std;int main(){ int ans; string s; while(cin>>s&&s[0]!='0') { ans=0; string::size_type k=s.size(); while(k) ans+=s[--k]-'0'; cout<<(ans-1)%9+1<<endl; } return 0;}
- hduoj1013,Digital Roots,又见数论,后悔高中没好好学奥数了
- hdu1013 Digital Roots 数论
- HDU1013-Digital Roots-数论(树根)
- HDU 1013 Digital Roots(数论)
- 数论---HDU 1163 Eddy's digital Roots
- HDU 1163 Eddy's digital Roots(数论)
- hdu 1163 Eddy's digital Roots (数论)
- Eddy's digital Roots-数论基础
- hdu 1013 Digital Roots(模拟 || 数论)
- HDU1163 Eddy's digital Roots(数论)
- ACM 数论 hdu 1013 Digital Roots
- HDU1013 Digital Roots 模拟&数论(我不会数论)
- HDOJ 1163 Eddy's digital Roots [简单数论]
- [ACM - 数论]Eddy's digital Roots(九余数定理)
- HDOJ 1163 Eddy's digital Roots [简单数论]
- HDOJ 1163 Eddy's digital Roots(简单数论)
- POJ 1519 && HDU 1013 Digital Roots(数论)
- HDU 1013.Digital Roots【模拟或数论】【8月16】
- 美国副国务卿本月16日至17日将访华-亚太局势-中日关系-钓鱼岛问题
- 银行业务调度系统学习总结
- 音视频解码
- hdu 1171
- 仿制金山毒霸专杀工具界面
- hduoj1013,Digital Roots,又见数论,后悔高中没好好学奥数了
- strcpy、strlen、strcmp函数
- 各地少先队集中开展红领巾心向党主题队日-少先队-主题队日-红领巾心向党
- 【阅读笔记之三】《DIRECTX.9.0.3D游戏开发编程基础》:Direct3D初始化
- 问题
- 星号图,自选一
- GBK编码表
- 提高Flex程序性能的10个秘诀
- linux 命令 ln 详解