Recursion for age

Source: Internet
Author: User

1 /*
2. Question: How old are five people sitting together? He said he is two years older than 4th. Asked 4th people about the age, he said
Three or three people are two years old. Ask the third person, and say that the person is two years older than 2nd. Ask 2nd people, saying they are two years older than the first. Last
4. Ask the first person, he says he is 10 years old. How old is the fifth person?
5
6. Program Analysis: Using recursive methods, recursion is divided into two stages: push-back and recursion. To know the age of the fifth person, you need to know
7. The age of the fourth person, and so on, is pushed to the first person (10 years old) and then back.
8 */
9 # include <stdio. h>
10
11 int age (int n)
12 {
13 int c;
14
15 if (n = 1)
16 return 10;
17
18 else
19 {
20 c = age (n-1) + 2;
21 return c;
22}
23}
24
25 int main ()
26 {
27 // int I;
28
29 printf ("his age is: % d \ n", age (5 ));
30
31 // for (I = 1; I <6; I ++)
32 // printf ("the % d man is: % d \ n", I, age (I ));
33
34 return 0;
35}


 




From zhengmian

Related Article

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.