当前位置:网站首页>【函数与递归】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;
}边栏推荐
猜你喜欢

idea uses @Autowired annotation to explain the reasons and solutions

typescript46-函数之间的类型兼容性

Exception (abnormal) and Error (error) difference analysis

typescript39-class类的可见修饰符

【特征选取】计算数据点曲率

【转】最小描述长度准则MDL(Minimun Description Length)

用pulp库解决运输问题【详细】

3. 无重复字符的最长子串

Build your own web page on the Raspberry Pi (2)

Modelarts第一次培训
随机推荐
阿凡提的难题
Build your own web page on the Raspberry Pi (2)
Newifi路由器第三方固件玩机教程,这个路由比你想的更强大以及智能_Newifi y1刷机_smzdm
D-PHY
Get the Ip tool class
背压机制
4.如何避免缓存穿透、缓存击穿、缓存雪崩
Odps temporary query can write SQL, turned out to a named?
web安全-sql注入漏洞
Djiango第三次培训
曲线特征----曲线弯曲程度的探究
-寻找鞍点-
Power button 561. An array of split
《录取通知》 观后感
1058 选择题 (20 分)(C语言)
mysql 存储过程 动态参数 查询执行结果
junit总结
Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data
js实现一个 bind 函数
Tag stack - stack monotonically preparatory knowledge - lt. 739. The daily temperature