当前位置:网站首页>Recursive function C language question type
Recursive function C language question type
2022-04-21 10:35:00 【jeff one】
Recursive function C Language Question type
1. subject : Yes 5 A person sits together , Ask No 5 How old is an individual ? He said bidi 4 Personal big 2 year . Ask No 4 Personal age , He said bidi 3 Personal big 2 year .
Ask No 3 personal , And he said bidi 2 People are two years old . Ask No 2 personal , Say bidi 1 I'm two years older . Last Ask No 1 personal , He says it is 10 year . May I ask 5 How old are you ?
Program analysis : Using recursive methods , Recursion can be divided into two stages: backward and recursive . If you want to know the second 5 Personal age , Need to know the second 4 People's age , By analogy , Push it to the second 1 people (10 year ), Push back .
#include <stdio.h>
int age(a)//a For whom
{
int i;//i Used to store age
if(a==1)
{
i=10;
return i;
}
else
{
i=age(a-1)+2;
printf(" The first %d The number of rounds is %d\n",a,i);
return i;
}
}
int main()
{
int Age=age(5);
printf(" The first 5 An individual's age is %d year ", Age);
return 0;
}
2. subject : Calculation 1+2+3+…+100
#include <stdio.h>
int func(int a)
{
int b;
if(a>0)
{
b=a+func(a-1);
}
return (b);
}
int main()
{
int a=100,b;
b=func(a);
printf("1+2+3+...+100 The value of is %d",b);
}
3. Calculation 10 The factorial ( You can rewrite... Based on the previous question )
Tips : here 10 Don't change it again ,10 The factorial of is already a large number .
#include <stdio.h>
int func(int a)
{
static unsigned long long int c;
if(a==0)
{
return 1;
}
else
{
c=a*func(a-1);
}
return (c);
}
int main()
{
int a=10;
unsigned long long int b;
b=func(a);
printf("100! The value of is %d",b);
}
4. Fibonacci sequence
Fibonacci sequence is the third number, which is equal to the sum of the first two numbers .
#include <stdio.h>
int fibonaci(int i)
{
if(i == 0)return 0;
if(i == 1)return 1;
return fibonaci(i-1) + fibonaci(i-2);
}
int main()
{
int i;
for (i = 0; i < 10; i++)
{
printf("%d\t\n", fibonaci(i));
}
return 0;
}
版权声明
本文为[jeff one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211028283345.html
边栏推荐
猜你喜欢

Usage of SAP ABAP for all entries

MKL与VS2019配置方法

Design a high-quality API interface

Zsh: segmentation fault solution

2022 information and future preparation 3 new online judge 1059: string sorting

英伟达、万向、风语筑、追梦者基金……大咖漫谈“元宇宙与产业新机遇”丨2022元宇宙云峰会...

24张图攻克border-image

2-3. 注册selector

基于润和大禹开发板的导购系统项目方案

【经验】HP打印机的IP为什么是169.254开头的
随机推荐
24 pictures to conquer border image
(SIP-1-话机注册)关于IP话机通过SIP协议注册到PBX电话交换机的全过程解析-如何看wireshark中的报文
C#入门-并行编程
塔米狗知识|上市公司收购的基本原则
The shell script modifies the file name and permissions
Talk about the safety factor of futures online account opening
2022信息与未来预备刷题4《New Online Judge 2034: [蓝桥杯2022初赛] 修剪灌木》
以用户体验五要素的思路,如何编写产品需求文档(PRD)
Showcase时手机不够怎么办? 云真机平台atxserver2
数字经济-新经济指数(2017-2022)&31省数字经济测算(2013-2020)两大维度指标
两两数之和
PHP文件包含:require,require_once;include,include_once
MKL and vs2019 configuration method
2022 information and future preparation topic 2 new online judge 1113: digit problem
利用随机函数实现随机范围的改变
Summary of knapsack problem (0-1, complete, multiple knapsack problem)
Opencv - contour detection
SAP ABAP FOR ALL ENTRIES 的用法
趣丸集团招股书“失效”,旗下TT语音已下架,如何实现稳定增长?
24 pictures to conquer border image