Home > Back-end >  Ask a question
Ask a question

Time:03-24

# include & lt; stdio.h>
Long f (n)
{
If (n!=0)
Return (n % 10 + f (n/10));
The else
return 0;
}
Int main ()
{
Long n=1234567890;
Printf (" % ld \ n ", f (n));
return 0;
}
The answer to the C program is 45, but I don't understand how operation, have a great god help me solve

CodePudding user response:

Recursion, equivalent to the n=1234567890 individual bits together

CodePudding user response:

Recursive function, n=1234567890, each number, the sum of 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9=45. Return (n % 10 + f (n/10)); N % each number 10, f (10) n/n under a number,

CodePudding user response:

Fn (123456789)===& gt;
9 + fn (12345678)
9 + 8 + fn (1234567);
9 + 8 + 7 (123456);
9 + 8 +... 1;

CodePudding user response:

refer to the second floor QZJHJXJ response:
recursive functions, n=1234567890, obtained each number, the sum of 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9=45. Return (n % 10 + f (n/10)); N % each number 10, f (10) n/n under a digital,

See, thank you

CodePudding user response:

reference 1/f, a white steamed bun response:
recursion, equivalent to the n=1234567890 each seat together

See, thank you

CodePudding user response:

reference 3 floor truth is right or wrong response:
fn (123456789)===& gt;
9 + fn (12345678)
9 + 8 + fn (1234567);
9 + 8 + 7 (123456);
9 + 8 +... 1;

Understand, thank you
  • Related