hdu-1013 Digital Roots
来源:互联网 发布:音响 知乎 编辑:程序博客网 时间:2024/06/03 14:31
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
Source
Greater New York 2000
题意:将输入数各位数相加,如果结构大于等于10,重复各位数相加步骤。
如24,2+4=6小于10输出, 39 3+9=12,大于等于10,重复相加步骤1+2=3。
此题陷阱在于输入的数可以为一个无限大的数,所以排除了用int和longlong装数据的想法。
对于大于longlong范围的无限大的数,只能用字符串来存放了。
#include <stdio.h>#include <string.h>char a[10010];int main(){ int n,digit,sum = 0,i; while (scanf("%s",a)!=EOF) { if(strcmp(a, "0")==0) return 0; sum=0; for(i=0;i<strlen(a);i++) sum=sum+a[i]-'0'; while(sum>=10){ n=sum; sum=0; do{ digit=n%10; sum=sum+digit; n=n/10; }while(n!=0); n=sum; } printf("%d\n",sum); }}
AC代码
阅读全文
0 0
- HDU 1013 Digital Roots
- HDU 1013 Digital Roots
- hdu 1013 Digital Roots
- HDU 1013 Digital Roots
- HDU 1013 Digital Roots
- HDU 1013 Digital Roots
- hdu 1013 Digital Roots
- HDU 1013 Digital Roots
- hdu 1013 Digital Roots
- HDU--1013--Digital Roots
- hdu 1013 Digital Roots
- hdu 1013 Digital Roots
- HDU 1013Digital Roots
- HDU 1013 Digital Roots
- hdu 1013 Digital Roots
- hdu 1013 Digital Roots
- Digital Roots hdu 1013
- HDU 1013 Digital Roots
- 基于Qt QGraphicsView的多点触摸绘图
- 关于idea
- Java Web
- 详细讲解二叉树三种遍历方式的递归与非递归实现
- eclipse 导入mvn 项目 阻塞 installing dynamic web module facet
- hdu-1013 Digital Roots
- 对RecyclerView的item添加点击事件
- 多条目
- 数据结构实验:连通分量个数
- 索引优化
- 应用位异或交换两个变量的值
- spring bean 声明学习
- 【C
- I/O复用