HDU 1013 Digital Roots
来源:互联网 发布:midi键盘软件 编辑:程序博客网 时间:2024/06/11 18:47
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
思路:数字可能很大,要用数组存,然后直接%9。
代码:
#include <stdio.h>#include <string.h>char num[5555555];int main() { while (gets(num) && strcmp(num, "0")) {int ans = 0;for (int i = 0; i < strlen(num); i ++) { ans = (ans * 10 + num[i] - '0') % 9;}if (ans == 0) printf("9\n");else printf("%d\n", ans); } return 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
- 嵌入系统中标准输入输出扩展
- 调度器中如何使用短信推送功能
- Git使用之——冲突解决一(git merge conflict)
- php 常用自定义方法
- cocos2d-x帧动画 -- 角色跑动
- HDU 1013 Digital Roots
- FutureTask一例
- 抽象类小结
- 指针的魅力
- 题目1000:计算a+b
- android之SlidingMenu滑动菜单栏使用介绍
- 双机调度问题
- real,user and sys 进程时间统计
- (HLS播放器之一)HLS协议之M3U8解析