HDU 1013 Digital Roots(字符串,大数,九余数定理)
Digital Roots
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 79180 Accepted Submission(s):
24760
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.
integers, one per line. The end of the input will be indicated by an integer
value of zero.
on a separate line of the output.
39
0
#include <stdio.h>
#include<string.h>
int main()
{
char a[];
int sum;
while(scanf("%s",&a)!=EOF)
{
if(strlen(a)==&&a[]=='')
break;
sum=;
for(int i=;i<strlen(a);i++)
{
sum=sum+a[i]-'';
if(sum>=)
sum=sum/+sum%;
}
printf("%d\n",sum);
}
return ;
}
/*因为输入的数字可能很长,而一个数的digital root和该数每一位的和的digital root相等,所以可以直接算出所输入的数字的和(不会超过范围),然后根据常规方法求digital root。
*/
#include <stdio.h>
#include <string.h> int root(int x)
{
int r=;
while (x)
{
r+=x%;
x/=;
}
if (r<)
return r;
else
return root(r);
} int main()
{
char c;
int r=;
while (c=getchar(),)
{
if (c=='\n')
{
if (r==)
break;
printf("%d\n",root(r));
r=;
}
else
{
r+=c-;
}
}
}
//9余数定理 !!
#include<stdio.h>
#include<string.h>
char a[];
int main()
{
int n;
while(~scanf("%s",a),strcmp(a,""))
{
int sum=;
int len=strlen(a);
for(int i=;i<len;++i)
sum+=a[i]-'';//字符化为数字
printf("%d\n",(sum-)%+);
}
return ;
}
HDU 1013 Digital Roots(字符串,大数,九余数定理)的更多相关文章
- HDU 1013 Digital Roots(字符串)
Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...
- HDU 1013 Digital Roots(to_string的具体运用)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1013 Digital Roots Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1013 Digital Roots【字符串,水】
Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1013.Digital Roots【模拟或数论】【8月16】
Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...
- HDU 1013 Digital Roots 题解
Problem Description The digital root of a positive integer is found by summing the digits of the int ...
- hdu 1013 Digital Roots
#include <stdio.h> int main(void) { int m,i;char n[10000]; while(scanf("%s",&n)= ...
- HDU OJ Digital Roots 题目1013
/*Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU - 1310 - Digital Roots
先上题目: Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 杭电 1013 Digital Roots
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1013 反思:思路很简单,但是注意各位数加起来等于10的情况以及输入0的时候结束程序该怎么去表达 #in ...
随机推荐
- 解决hadoop 集群启动常见错误办法
hadoop 集群常见错误解决办法 hadoop 集群常见错误解决办法: (一)启动Hadoop集群时易出现的错误: 1. 错误现象:Java.NET.NoRouteToHostException ...
- phaser常用API总结
1. 游戏画布的尺寸 var width = game.width, height = game.height; 2. 中心点坐标 var game = new Phaser.Game(...); ...
- 门户系统整合sso cookie共享及显示用户信息
1.1 门户系统整合sso 在门户系统点击登录连接跳转到登录页面.登录成功后,跳转到门户系统的首页,在门户系统中需要从cookie中 把token取出来.所以必须在登录成功后把token写入cooki ...
- Intellij Idea debug 远程部署的的tomcat项目
web项目部署到tomcat上之后,有时需要打断点单步调试,如果用的是Intellij idea,可以通过如下方法实现: 开启debug端口,启动tomcat 以tomcat7.0.75为例,打开bi ...
- 用boost::lexical_cast进行数值转换
在STL库中,我们可以通过stringstream来实现字符串和数字间的转换: int i = 0; stringstream ss; ss << "123"; ...
- POJ2391:Ombrophobic Bovines(最大流+Floyd+二分)
Ombrophobic Bovines Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 21660Accepted: 4658 题目 ...
- 生日蛋糕 POJ - 1190
7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1 <= i <= M)层蛋糕是半径为Ri, 高度为Hi的圆柱.当 ...
- Ubuntu14.04 换源 阿里云
sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup sudo vim /etc/apt/sources.list sudo apt-g ...
- 忘记mysq rootl密码后解决办法
如果mysql正在运行,/etc/init.d/mysqld stop 启动mysql(无需输入密码):bin/safe_mysqld –skip-grant-tables & 在bin目录下 ...
- hadoop更换硬盘
hadoop服务器更换硬盘操作步骤(datanode hadoop目录${HADOOP_HOME}/bin 日志位置:/var/log/hadoop)1.登陆服务器,切换到mapred用户,执行 ...