Hdoj (1013)--digital Roots (String simulation)

Source: Internet
Author: User

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

In the final analysis, it is important to note that it is possible to give a large number of numbers, so you cannot use int or __int64 to store the string.

#include <stdio.h> #include <string.h> #include <iostream>using namespace Std;int main () {    char a[ 1000],B[1000];    int i,j,k,l,ans,t;    while (scanf ("%s", a)!=eof) {        if (a[0]== ' 0 ') is break  ;        L=strlen (a);         strcpy (b,a);         while (1) {            ans=0;            for (i=0;i<l;i++) {                ans+=b[i]-' 0 ';            }            if (ans>9) {for                (i=0;ans!=0;i++) {                    b[i]= (ans%10) + ' 0 ';                    ANS=ANS/10;                }                b[i]= ' + ';            }            else break;            L=strlen (b);        }        printf ("%d\n", ans);}    }


Hdoj (1013)--digital Roots (String simulation)

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.