Digital Roots

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 79180    Accepted Submission(s):
24760

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.

 
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
24
39
0
 
Sample Output
6 3
 #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(字符串,大数,九余数定理)的更多相关文章

  1. HDU 1013 Digital Roots(字符串)

    Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...

  2. HDU 1013 Digital Roots(to_string的具体运用)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1013 Digital Roots Time Limit: 2000/1000 MS (Java/Othe ...

  3. HDU 1013 Digital Roots【字符串,水】

    Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. HDU 1013.Digital Roots【模拟或数论】【8月16】

    Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...

  5. HDU 1013 Digital Roots 题解

    Problem Description The digital root of a positive integer is found by summing the digits of the int ...

  6. hdu 1013 Digital Roots

    #include <stdio.h> int main(void) { int m,i;char n[10000]; while(scanf("%s",&n)= ...

  7. HDU OJ Digital Roots 题目1013

     /*Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU - 1310 - Digital Roots

    先上题目: Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  9. 杭电 1013 Digital Roots

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1013 反思:思路很简单,但是注意各位数加起来等于10的情况以及输入0的时候结束程序该怎么去表达 #in ...

随机推荐

  1. 做一个iframe的弹出框

    群里有个人想在微信页面里面加弹出框.作为前端的我,想着不可能这样做.后来一个人说了: A:如果对方没有防盗链的话,你可以建个页面,内置iframe 到他的页面,然后把url 的参数也传入你的ifram ...

  2. JavaScript 操作选中当前的li元素并给他添加select类

    JavaScript 操作选中当前的li元素并给他添加select类.之前都是使用jquery写的,今天使用JavaScript写一个. <!DOCTYPE html> <html ...

  3. BJOI2018

    BJOI2018 省选挂完,是时候更一篇题解了.对于鬼畜结论题我只放结论不给证明,不要打我-- day1 二进制 试题描述 pupil 发现对于一个十进制数,无论怎么将其的数字重新排列,均不影响其是不 ...

  4. [IOI2007 D1T1]Miners 矿工配餐

    题目大意:有$2$个煤矿,$n$天.每天给一个煤矿送餐(共有有$3$种餐),价值为它与前面两次送餐(如果有的话)不同的种类数.最大化价值. 题解:看到只有三种餐,考虑状压$DP$.$f_{i,j,k, ...

  5. [ZJOI2005]沼泽鳄鱼 矩阵乘法

    ---题面--- 题解: 乍一看还是挺懵逼的.和HH去散步很像,思路也是类似的. 复制一段我在HH去散步的题解里面写的一段话吧: 考虑f[i][j]表示i和j是否右边相连,有为1,否则为0,那么f同时 ...

  6. [Leetcode] subsets ii 求数组所有的子集

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  7. git使用笔记(三)文件忽略

    By francis_hao    Nov 19,2016 注:此条所有内容均来自$ git help gitignore,细节请参考之   有时候在仓库里有一些文件我们并不想提交,git提供了指定屏 ...

  8. HDU 1203 01背包

    I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. Math.abs为Integer.Min_VALUE返回错误的值

      Math.abs为Integer.Min_VALUE返回错误的值 这段代码: System.out.println(Math.abs(Integer.MIN_VALUE)); 回报-2147483 ...

  10. 【STSRM12】整除

    [题意]给定长度为n的序列A,求最长的区间满足区间内存在数字能整除区间所有数字,同时求所有方案.n<=5*10^5,Ai<2^31. [算法]数论??? [题解]首先一个区间的基准数一定是 ...