HDU 1013
来源:互联网 发布:趣学python编程中文pdf 编辑:程序博客网 时间:2024/06/06 02:10
Digital Roots
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 78456 Accepted Submission(s): 24533
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//计算给定的数值位数加起来mod 9 之后的值#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;int main(){ string s; while (cin >> s && s != "0") { int sum = 0; for(int i = 0;i < s.size(); ++i) sum += s[i] - '0'; cout << (sum - 1) % 9 + 1 << endl; } return 0;}//C语言#include <stdio.h>#include <string.h>int caculate(long long n){ while(n >= 10){ n = n/10+n%10; } return n;}int main(){ char s[1050]; while(gets(s)&&s[0]!='0'){ int sum = 0,i; for(i = 0 ; i < strlen(s);i++) sum+=s[i]-'0'; printf("%d\n",caculate(sum)); } return 0;}
阅读全文
0 0
- hdu 1013
- hdu 1013
- hdu 1013
- HDU 1013
- hdu 1013
- hdu 1013
- hdu 1013
- HDU-1013
- hdu 1013
- HDU 1013
- HDU 1013
- HDU 1013
- hdu 1013
- hdu 1013
- HDU-1013
- HDU 1013
- hdu 1013
- HDU 1013
- J2EE系列之SpringMVC学习笔记(一)--SpringMVC简介
- iOS开发:isKindOfClass、isMemberOfClass和isSubclassOfClass的使用
- 搭建:canal部署与实例运行和解析MysqlBinlog日志发送到Kafka中
- 为什么要选择react
- org.apache.spark.sql.api.java.JavaSQLContext
- HDU 1013
- maven管理的web项目引入本地jar包的部署
- 轻量化软件设计五大法则
- Centos搭建SFTP服务器
- 网络的那些事之ACL
- 高性能Mysql笔记 (4) --库表结构优化
- 基于canvas的图像有机物检测
- 解决ajax跨域问题的多种方法
- 记录