这本阿里P8撰写的算法笔记,再次推荐给大家,身边不少朋友学完这本书最后加入大厂:
Github 疯传!史上最强悍!阿里大佬「LeetCode刷题手册」开放下载了!
学习函数主要学习的就是函数的声明、定义和调用,下面请看两个例子,来帮助我们学习函数:
题目一:
编写一个函数iswithin(),它接受两个参数,一个是字符,另一个是字符串指针。其功能是如果字符在字符串中。就返回1 (真);如果字符不在字符串中,就返回0(假)。在一个使用循环语句为这个函数提供舒服的完整程序中进行测试。
代码如下:
#include <stdio.h>
int iswithin(char p,char q) { while(q) { if(p == q) return 1; else q++; } return 0; } int main(int argc, char *argv[]) { int m; char p,q; p = *argv[1]; q = argv[2];
m = iswithin(p,q);
if(m == 1)
printf("\'%c\' is in the string!\n",p);
else
printf("\'%c\' is not in the string!\n",p);
return 0;
} 执行结果如下:
fs@ubuntu:/qiang/hanshu$ ./hanshu2 h hello 'h' is in the string! fs@ubuntu:/qiang/hanshu$ ./hanshu2 h world 'h' is not in the string! fs@ubuntu:~/qiang/hanshu$ 注意函数传参的方式。
题目二、
以下函数的功能是用递归的方法计算 x 的 n 阶勒让德多相式的值。已有调用语句p(n,x):编写函数实现功能。
代码如下:
#include <stdio.h>
int p(int n,int x) { int m;
if(n == 0)
return 0;
else
if(n == 1)
return x;
else
{
m = ((2*n - 1)*x*p(n - 1,x) - (n - 1)*p(n - 2,x))/n;
return m;
}
}
int main(int argc, const char *argv[]) { int x, n; int q; printf("Please input x and n:\n"); scanf("%d%d",&x,&n); q = p(n,x);
printf("p = %d\n",q);
return 0;
} 执行结果如下:
fs@ubuntu:/qiang/hanshu$ ./hanshu1 Please input x and n: 2 1 p = 2 fs@ubuntu:/qiang/hanshu$ ./hanshu1 Please input x and n: 2 5 p = 194 fs@ubuntu:~/qiang/hanshu$
———————————————— 版权声明:本文为优快云博主「zqixiao_09」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.youkuaiyun.com/zqixiao_09/article/details/50373456
这本阿里P8撰写的算法笔记,再次推荐给大家,身边不少朋友学完这本书最后加入大厂:
Github 疯传!史上最强悍!阿里大佬「LeetCode刷题手册」开放下载了!
以上就是良许教程网为各位朋友分享的Linux相关知识。