HDU 1013.Digital Roots "analogue or number Theory" "August 16"

Source: Internet
Author: User

Digital Roots

Problem DescriptionThe 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.

Inputthe 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.

Outputfor each integer in the input and output its digital root on a separate line of the output.

Sample Input
24390

Sample Output
63
a number, the number of single-digit addition to the output, if less than 10, or continue to get the number of individual digits added. I do the simulation at the first glance. When simulating, it is important to note that the number entered may be very large, so it is not possible to use int, which should be handled by string. The simulation procedure code is as follows:

#include <cstdio> #include <cstring>void zuo (int x) {    int sum=0;    while (x) {        sum+= (x%10);        x/=10;    }    if (sum<10) printf ("%d\n", sum);    else Zuo (sum);} int main () {    char s[1010];    while (scanf ("%s", s) &&s[0]!= ' 0 ') {        int x=0;        for (int i=0;i<strlen (s); i++)            x+= (s[i]-' 0 ');        Zuo (x);    }    return 0;}
There is also a solution to the knowledge of number theory.

Number itself: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 12 22 23 24 25 26 27 28 29 30 ··········

Single digit and: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3

You will find that every 9 is a loop, so as long as the 9 is OK. The code is as follows:

#include <cstdio> #include <cstring>int main () {    char s[1010];    while (scanf ("%s", s) &&s[0]!= ' 0 ') {        int x=0;        for (int i=0;i<strlen (s); i++)            x+= (s[i]-' 0 ');        x=x%9;        if (x==0) printf ("9\n");        else printf ("%d\n", x);    }    return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1013.Digital Roots "analogue or number Theory" "August 16"

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.