数学/hdu 1013 Digital Roots

题意

  求一个数的数根,即各位数之和,再之和,直到为个位数

分析

首先,要知道这样一个结论:

    任何一个整数模9同余于它的各数位上数字之和

  具体证明过程如下:

  设自然数N=a[n]a[n-1]…a[0],其中a[0],a[1]、…、a[n]分别是个位、十位、…上的数字

  再设M=a[0]+a[1]+…+a[n]

  求证:N≡M(mod 9).

 证明:
     ∵ N=a[n]a[n-1]…a[0]=a[n]*10^n+a[n-1]*10^(n-1)+…+a[1]*10+a[0].
    又∵ 1≡1(mod 9),
        10≡1(mod 9),
        10^2≡1(mod 9),
          … 
        10^n≡1(mod 9).
    上面这些同余式两边分别同乘以a[0]、a[1]、a[2]、…、a[n],再相加得:
      a[0]+a[1]*10+…+a[n]*10^n≡(a[0]+a[1]+…+a[n])(mod 9),
                    即 N≡M(mod 9),得证。

  有了这个性质就容易解决本题了

  在计算过程中,可以不断mod 9,因为我们知道有这样两个性质:

    (A+B)mod C = ((A mod C) + (B mod C))mod C   
    (AB)mod C = ((A mod C)×(B mod C)) mod C

 还要注意,如果余数为0,则输出9

  注意:存不下 用字符串

  以上直接复制粘贴的我另一个blog 两年前的了 没想到如今自己又开始做题了

Accepted Code

 1 /*
 2     PROBLEM:hdu1013
 3     AUTHER:Nicole Lam
 4     MEMO:数学
 5 */
 6 #include<iostream>
 7 #include<cstring>
 8 #include<string>
 9 using namespace std;
10 int main()
11 {
12     string s;
13     while (cin>>s && s[0]!=‘0‘)
14     {
15         int sum=0;
16         for (int i=0;i<s.size();i++)
17         {
18             sum+=s[i]-‘0‘;
19             sum=sum%9;
20         }
21         if (sum==0) sum=9;
22         cout<<sum<<endl;
23     }
24     return 0;
25 }
时间: 2024-12-21 13:15:54

数学/hdu 1013 Digital Roots的相关文章

HDU 1013 Digital Roots

算法爱好者 HDU 1013 Digital Roots Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29353    Accepted Submission(s): 8974Problem Description The digital root of a positive integer is foun

HDU 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

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

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1013 Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 90108    Accepted Submission(s): 28027 Problem Description The digital root of a

HDU 1013 Digital Roots【字符串,水】

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

HDU 1013 Digital Roots(两种方法,求数字根)

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

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

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,

hdu 1013 Digital Roots 用一个大水题来纪念我进入杭电前一万名

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

HDU 1013 Digital Roots(九余数定理)

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

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 Problem Description The digital root of a positive integer is found by summing the digits of