"Hdu" 1013 number root (C language)

Source: Internet
Author: User
-------I small white one, just contact programming soon, have what question also please enlighten us-------

The problem description finds the number root of a positive integer by summing the number of integers. If the result value is a single number, then the number is the root of the number. If the result value contains two or more digits, add the numbers and repeat the process. As long as it is necessary to obtain a number, it will continue to do so.

For example, consider a positive integer of 24. Add 2 and 4 to produce a value of 6. Since 6 is a single digit, 6 is the number root of 24. Now consider the positive integer 39. Add 3 and 9 12. Because 12 is not a single number, you must repeat the procedure. Add 1 and 2 3, a number, and also a number root of 39.
The input file will contain a list of positive integers, one for each line. The end of the input is represented by an integer value of 0.
Outputs a number root for each integer in the input to the individual line of the output.

Sample input

24 39 0 Sample output
6 3

Ideas for solving problems:

This problem mainly consists of two steps

STEP1: Add all the integers, less than 9 direct output

STEP2: More than 9, consider continuing the cycle calculation until less than 9, here is a double loop, see Code

Attention

1. The integer input can be very long, so you cannot use int or long, where you need to consider the use of string input and convert the string to value num + = (str[i]-' 0 ');

2. Note that the end of the entry required to enter a 0, using the loop: while (scanf ("%s", str) && str[0]!= ' 0 ')

Code 1 (accepted)

#include <iostream>

int main ()
{
    int num, ans;
    Char str[1000];
    while (scanf ("%s", str) && str[0]!= ' 0 ')
    {
        num = 0;
        for (int i=0; str[i]!= '; i++)
            num + = (str[i]-' 0 ');
        while (num>9)
        {
            ans = 0;
            while (num)
            {
                ans = num%10;
                Num/=
            }
            num = ans;
        printf ("%d\n", num);
    }
    return 0;
}

Code 2 (Wrong Answer)

Although this does not pass, but I think the first thing to express is to enter all integers, the ' 0 ' end.

But what actually passes is the input of an integer, which outputs an integer form. (I don't know if it's not me where I understand it wrong

#include <stdio.h>
int main ()
{
    int num, ans;
    Char str[1000];
    int result[1000];//defines an array to store the result
    int x=0;        Defines a variable to hold the number of outputs while
    (scanf ("%s", str) && str[0]!= ' 0 ')
    {
        num = 0;
        for (int i=0; str[i]!= '; i++)
            num + = (str[i]-' 0 ');
        while (num>9)
        {
            ans = 0;
            while (num)
            {
                ans = num%10;//single-digit
                num/= 10;//10, Hundred, Thousand ...
            }
            num = ans;
        Result[x]=num;        The calculated results are saved to the result array in
        x + +;                The number of output results plus one
        
    }
    int i=0;
    printf ("The results are as follows: \ n");
    for (i=0;i<x;i++)
        printf ("%d\n", Result[i]);
    
    return  0;
}


In this piece of code I think the 13--22 line is the breakthrough point of this problem.

-------------------------------------I'm a split line-------------------------------------------

The following is I do the back "1163" when the use of more than nine theorems, think of the nine-number theorem can be more simple to solve the problem, the result is very happy to pass the ~

Code:

#include <stdio.h>


int main ()
{
    int num,temp;
    Char str[1000];
    while (scanf ("%s", str) && str[0]!= ' 0 ')
    {
        num = 0;
        for (int i=0; str[i]!= '; i++)
            num + = (str[i]-' 0 ');
        Temp=num;
        temp=temp%9;
        if (temp==0)
            printf ("9\n");
        else
            printf ("%d\n", temp);
    }
    return 0;
}

Do you have any other methods, welcome to the message ~

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.