Algorithm training first question

Source: Internet
Author: User

/*


The number of roots can be obtained by adding a number of digits to each digit. If the resulting number is a single number, then this number is the number root. If the result is a double-digit number or a number that includes more digits, add the numbers together. So go on until you get a count.

For example, for 24来 said, add 2 and 4 to get 6, because 6 is a single number, so 6 is the number of 24. For example, 39, add 3 and 9 to get 12, because 12 is not a number, so we have to add 1 and 2 together, and finally get 3, which is a single number, so 3 is the number of 39.
Input
A positive integer (less than 10 1000).
Output
A number, which is the number root of the input number.
Sample Input
24
Sample Output
6


*/
#include <stdio.h>
int main ()

{
Char a[3000];
int sum=0,i=0,fun=0,j;
Gets (a);
while (a[i]!= ')
{


sum+=a[i]-' 0 '; Converts a long string of strings to a data type that can be stored in int type
i++;

}

while (1)
{
fun=0;
if (sum==sum%10)
{
Fun+=sum;
Break
}
if (sum!=sum%10)
{while (sum)
{
fun+=sum%10;
SUM=SUM/10;
}
}
J=fun;
if (j!=j%10)
Sum=j;
Else
Break
}
printf ("%d", fun);

}


The main difficulty of this question is how to express the number of 10 of the 1000 square

1. Using each number in a single character space

2. The number of these numbers can be stored in the data of type int

3. Finally add up on the line.

Algorithm training first question

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.