hdoj 1013Digital Roots



/*Digital Roots

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*/

<span style="font-size:18px;">#include <stdio.h>
#include <string.h>
int main()
{
 int sum;
 char a[1000],n[1000];
 while(gets(n))
 {
  if (strcmp(n,"0")==0)
  {
   break;
  }
  else
  {
   int sum=0;
   for (int j=0;j<strlen(n);j++)
   {
                sum+=(n[j]-48);
   }
   while (sum>=10)
   {
    sprintf(a,"%d",sum);//把格式化的数据写入某个字符串缓冲区。意思是把sum转化为字符串a。
    sum=0;
    for (int i=0;i<strlen(a);i++)
    {
     sum+=(a[i]-48);
    }
   }
     printf("%d\n",sum);
  }
 }
}</span>

这个方法不容易想到。

<span style="font-size:18px;">//9余数定理
#include<stdio.h>
#include<string.h>
char a[10010];
int main()
{
 int n;
 while(~scanf("%s",a),strcmp(a,"0"))
 {
  int sum=0;
  int len=strlen(a);
  for(int i=0;i<len;++i)
  sum+=a[i]-'0';
  printf("%d\n",(sum-1)%9+1);//为什么减一后再加一,是为了避免18这些数字。
 }
 return 0;
}
</span>

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-08 21:17:13

hdoj 1013Digital Roots的相关文章

ACM--数字位数相加--HDOJ 1013--Digital Roots--水

HDOJ 题目地址:传送门 Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 68962    Accepted Submission(s): 21568 Problem Description The digital root of a positive integer is found by summing

杭电1013--Digital Roots

Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 57905    Accepted Submission(s): 18093 Problem Description The digital root of a positive integer is found by summing the digits of

HDU 1013Digital Roots 吃完饭,水个题。

Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 53073    Accepted Submission(s): 16565 Problem Description The digital root of a positive integer is found by summing the digits of

HDOJ 1163 Eddy&#39;s digital Roots(简单数论)

[思路]:http://blog.csdn.net/iamskying/article/details/4738838 求解思路: 现在分析一个问题,假设将十位数为a,个位数为b的一个整数表示为ab,则推导得 ab*ab = (a*10+b)*(a*10+b) = 100*a*a+10*2*a*b+b*b 根据上式可得:root(ab*ab) = a*a+2*a*b+b*b = (a+b)*(a+b);[公式一] 同理也可证得:root(ab*ab*ab) = (a+b)*(a+b)*(a+b)

HDOJ(1013) ——Digital Roots(字符串模拟题)

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 a

HDU1013 POJ1519 Digital Roots(解法三)

该问题的最佳解法是利用数论的9余数定理来计算数根.一个数的数根等于该数的9的余数,若余数为0则结果为9. 问题链接:HDU1013 POJ1519 Digital Roots.入门练习题,用C语言编写程序. 问题简述:输入若干正整数,求其数根,直到输入为0为止. 问题分析:数根是指整数的各个位的数字之和.如果其和为1位整数,则为结果:如果其和为多位整数,则再将各位数字相加,直到其和为1位数为止.这个问题的大陷阱是,没有指出整数是多少位的.即使使用unsignde long long类型,也可能会

题目1124:Digital Roots (方法超简单)

题目1124:Digital Roots 学到的新知识 求一个数各个的和可以把其%9就行,例如13%9=4 11%9=2:123%9=6: 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3819 解决:1335 题目描述: 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

HDU 1163 Eddy&#39;s digital Roots

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5783    Accepted Submission(s): 3180 Problem Description The digital root of a positive integer is found by summing the digit

【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. 1 /* 4328 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector>