HDU 1013 Digital Roots__hdu

Source: Internet
Author: User
Digital Roots Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 50643 accepted Submission (s): 15810


Problem Description The digital root of a positive integer is found by summing the digits of the. If The resulting value is a single digit then this 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-digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since isn't 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 the 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 Nine method: The number of a number is equal to this number modulo 9, also equal to all digits of the sum modulo 9, 9 of the words directly output

#include <iostream>
#include <string>
using namespace Std;

int main ()
{
string S;
while (1)
{
cin>>s;
if (s== "0")
Break
int n=0;
for (int i=0;i<s.size (); i++)
{
n + = (s[i]-' 0 ');
n%= 9;
}
if (n==0)
cout<<9<<endl;
Else
cout<<n<<endl;
}
return 0;
}


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.