Hdu_1013 digital roots

Source: Internet
Author: User

Today, I am a little depressed. I want to solve the problem with two questions. I found this question and the result is .... Wa has been written for n times. At the beginning, we didn't take into account the large numbers. We wrote an integer to the end recursively, and then wa came down !! Later, I saw someone in discuss saying that it was written in strings. The result was wa for a long time. I couldn't help it. I looked at other people's code. I was dizzy! Ended with 0. I did not judge it directly .... A tragic day ~~~

Read the Code:

# Include <stdio. h>
# Include <string. h>
Int root (int x)
{
Int sum = 0;
While (X)
{
Sum + = x % 10; X/= 10;
}
If (sum <10) return sum;
Else return root (SUM );
}
Int main ()
{
Int X, I;
Char s [1001];
While (scanf ("% s", S )! = EOF)
{
If (! Strcmp (S, "0") break;
For (I = 0, x = 0; s [I]! = '\ 0'; I ++)
X + = s [I]-'0 ';
Printf ("% d \ n", root (x ));
}
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.