当前位置:网站首页>【函数与递归】7.19
【函数与递归】7.19
2022-08-03 05:11:00 【tt142】
从今天开始就以所学内容进行命名,再也不担心记不住数字哈哈
今天还是学了一些函数的内容,很少一点的收尾,主要接触到一种新的语句,递归
初识还是很有难度的对我来说,一点都不简单,学了五个小时也算是才刚刚入门
现在的知识真的是连C语言的门槛都还没看见。。
#include<stdio.h>
//史上最简单的递归
int main()
{
printf("hehe");
main()
return 0;
} //可能存在stack overflow的问题
//接受一个无符号整型,并且按顺序打印,例如1234->1 2 3 4
void print(int x)
{
if(x>9)
print(x/10);
printf("%d ",x%10);
}
int main()
{
unsigned int a =0;
scanf("%d",&a);
print(a);
return 0;
}
//编写函数,不允许创建临时变量,求字符串长度
int my_strlen(char* x)
{
if(*x != '\0')
return 1+my_strlen(x+1);
else
return 0;
}
int main()
{
char arr[]="dada";
int ret=my_strlen(arr);
printf("%d",ret);
return 0;
}
//描述第n个斐波那锲数:
/首先用递归的方法写,并且表明循环工作量
int count;
int Fib(int x)
{
if(x == 2)
count++;
if(x>2)
return Fib(x-2)*Fib(x-1);
else
return 1;
}
int main()
{
int n=0;
scanf("%d",&n);
int ret= Fib(n);
printf("%d",ret);
printf("%d",count);
return 0;
}
//发现效率低下,改成函数方式
int count;
int Fib(int x)
{
int a, b=0;
int c=1;
for(a=1,b=1,c=1;x>2;count++,x--)
{
c=a + b ;
a=b;
b=c;
}
return c;
}
int main()
{
int n =0;
scanf("%d",&n);
int ret =Fib(n);
printf("ret=%d\n",ret);
printf("count=%d",count);
return 0;
}
//求n阶乘
int Fac(int x)
{
if(x>1)
return x*Fac(x-1);
else
return 1;
}
int main()
{
int n=0;
scanf("%d",&n);
int ret = Fac(n);
printf("%d",ret);
return 0;
}
边栏推荐
猜你喜欢
Detailed explanation of MOSN reverse channel
设计模式——组合模式、享元模式(Integer缓存)(结构型模式)
网络流媒体下载的 10 种方法(以下载 Echo 音乐为例)
数据分析 第一篇
2.ROS通信机制
用scikit-learn学习谱聚类
D-PHY
typescript40-class类的保护修饰符
Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data
tag单调栈-单调栈预备知识-lt.739. 每日温度
随机推荐
Power button 561. An array of split
【Flask】Flask-SQLAlchemy的增删改查(CRUD)操作
Length n of condensed distance matrix ‘y‘ must be a binomial coefficient
typescript45-接口之间的兼容性
Talking about GIS Data (6) - Projected Coordinate System
《录取通知》 观后感
js实现一个 bind 函数
高效率科研神器——小软件、大能量
轨迹(形状)相似性判断与度量方法
2017-06-11 Padavan 完美适配newifi mini【adbyby+SS+KP ...】youku L1 /小米mini
VR全景展打造专属元宇宙观展空间
在树莓派上搭建属于自己的网页(1)
快速上手 Mockito 单元测试框架
idea uses @Autowired annotation to explain the reasons and solutions
【转】最小描述长度准则MDL(Minimun Description Length)
斐讯K2路由编译Padavan华硕固件和心得
IO process thread -> thread -> day5
typescript43-类型兼容性说明
breed Web刷机升级详细教材修正编译器固件说明_itkeji.top
PotPlayer实现上班摸鱼电视自由