HDU 1013 Digital Roots

Source: Internet
Author: User

Algorithmic enthusiasts

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): 8974

Problem Description

The digital root of a positive integer is found by summing the digits of the integer. If The resulting value is a, digit then, digit is the digital root. If The resulting value contains or more digits, those digits was summed and the process is repeated. This was continued as long as necessary to obtain a and 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 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 would contain a list of positive integers, one per line. The end of the input would be indicated by an integer value of zero.

Output

For each integer in the input, output it digital root on a separate line of the output.

Sample Input

24 39 0

Sample Output

6 3

Source

Greater New York 2000//Error procedure
int F1 () {int N,sum=0;while (cin>>n&&n) {while (1) {if (n<10) {cout<<n<<endl;break;} n = n/10+n%10;}} return 0;}

  

General Practice 1
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;int del (int a) {    int b=0;    while (a>0)    {       b+=a%10;       a/=10;    }    return b;} int main () {    char s[1003];    int i;    int A;    while (scanf ("%s", s), s[0]!= ' 0 ')    {   a=0;       for (i=0;s[i]!= '; i++)          a+=s[i]-' 0 ';        while (a>9)        {            A=del (a);        }        printf ("%d\n", a);    }    return 0;}

General Practice 2

int main (void) {int sum;string str;while (cin>>str) {sum = 0;if (str== "0") break;for (int i=0;i<str.size (); i++) { sum+=str[i]-' 0 ';} while (1) {if (sum<10) {cout<<sum<<endl;break;} sum = sum/10+sum%10;//The method has not been proved, the use of the law, a better way to see the conventional method 1}}return 0;}

  

Fast algorithm
Algorithm idea:
A modulus of 9 equals the numbers and modulo 9, for example 33%9=6%9;
Proof: a1a2a3...an%9= ((a1*10^n-1)%9+ (a2*10^n-2)%9 ...) %9
Right a1* (9999..9+1)%9=a1%9, etc. 、、、、、
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;int Main () {    char s[1003];    int i;    int A;    while (scanf ("%s", s), s[0]!= ' 0 ')    {   a=0;       for (i=0;s[i]!= ' n '; i++)          a=a*10+s[i]-' 0 ', a=a%9;        if (a==0) a=9;        printf ("%d\n", a);    }    return 0;}

  



HDU 1013 Digital Roots

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.