HDU1013——Digital Roots
来源:互联网 发布:蜂群算法代码 编辑:程序博客网 时间:2024/05/16 06:11
Digital Roots
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 69915 Accepted Submission(s): 21885
Total Submission(s): 69915 Accepted Submission(s): 21885
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.
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
解:题意为给定一个整数,如果该数是1位,则直接输出,两位或者两位以上,则把每一位相加,直到相加和为一位。
因为题目中并没有给定数的范围,所以可能无限大,使用字符串输入进行操作。
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int main(){string a;while(cin>>a && a!="0"){int sum=0;for(int i=0;i<a.length();i++){sum+=a[i]-'0';if(sum>9){sum=sum/10+sum%10;}}printf("%d\n",sum);}return 0;}
0 0
- Digital Roots—HDU1013
- HDU1013——Digital Roots
- ACM-水题之Digital Roots——hdu1013
- HDU1013 Digital Roots
- hdu1013 Digital Roots
- hdu1013 Digital Roots
- hdu1013(Digital Roots)
- HDU1013 Digital Roots
- hdu1013-Digital Roots
- hdu1013 Digital Roots
- hdu1013 Digital Roots
- hdu1013:Digital Roots
- hdu1013 Digital Roots 数论
- hdu1013 Digital Roots
- HDU1013 Digital Roots
- HDU1013 Digital Roots
- HDU1013 Digital Roots
- HDU1013 POJ1519 Digital Roots
- 翼发云赵裁:打造企业服务SaaS精品
- 蓝牙CC2541的串口通讯
- android 版本更新的应用
- Maven私服搭建Nexus Sonatype(四)
- ndk 独立(standalone)交叉编译环境搭建
- HDU1013——Digital Roots
- An invalid domain [.test.com] was specified for this cookie 原因分析
- 封装数据库操作
- DrySister看妹子应用(第一版)——1.项目搭建与简单实现
- zzuli 1895 985的0-1串难题
- ubuntu16.04安装后开发人员应该做的几件事儿
- Codeforces Round #365 (Div. 2)
- python变量问题
- 【CodeForces】703C - Chris and Road(思维,好题)