C语言练习题:函数部分(9题)
上一篇: C语言练习题:循环部分
下一篇: C语言练习题:数组部分
斐波那契,函数递归求法
/*
__author__ = 'xiaofeizhang'
函数作用:递归函数实现斐波那契
*/
#include <stdio.h>
int main(void){
long fibonacci(int n);
int num;
long sum;
printf("输入斐波那契数值:");
scanf("%d", &num);
sum = fibonacci(num);
printf("%d的斐波那契数列的数值为:%d", num, sum);
return 0;
}
long fibonacci(int n){
long sum;
if (n==0){
sum = 0;
}
else if (n==1){
sum = 1;
}
else {
sum = fibonacci(n-1) + fibonacci(n-2);
}
return sum;
}
/*
输入斐波那契数值:20
20的斐波那契数列的数值为:6765
--------------------------------
Process exited after 2.298 seconds with return value 0
请按任意键继续. . .
*/
计算一个公式
/*
__author__ = 'xiaofeizhang'
函数作用:函数实现计算一个公式
*/
#include <stdio.h>
int main(void){
double f(int n);
int num;
double sum;
printf("请输入n的值: ");
scanf("%d", &num);
sum = f(num);
printf("%d经过公式计算以后得到的值为:%f", num, sum);
return 0;
}
double f(int n){
double sum_part=0;
double sum=0;
for (int i=1; i<=n; i++){
for (int j=1; j<=i; j++){
sum_part += j;
}
sum += 1.0/sum_part;
sum_part = 0;
}
return sum;
}
/*
请输入n的值: 11
11经过公式计算以后得到的值为:1.833333
--------------------------------
Process exited after 1.115 seconds with return value 0
请按任意键继续. . .
*/
根据公式求p的值,结果由函数值带回
/*
__author__ = 'xiaofeizhang'
函数作用:根据公式求p的值,结果由函数值带回
*/
#include <stdio.h>
int main(void){
float f(int m, int n);
int m, n, t;
float result;
printf("输入m和n的值:");
scanf("%d,%d", &m, &n);
if (m<n){
t = m;
m = n;
n = t;
}
result = f(m, n);
printf("该函数的结果为 %f", result);
return 0;
}
float f(int m, int n){
double p;
long factorial(int value);
p = (factorial(m)*1.0)/(factorial(n)*factorial(m-n));
return p;
}
long factorial(int value){
long result;
if (value==0||value==1){
result = 1;
}
else {
result = value*factorial(value-1);
}
return result;
}
/*
输入m和n的值:12,8
该函数的结果为 495.000000
--------------------------------
Process exited after 6.617 seconds with return value 0
请按任意键继续. . .
*/
函数计算阶乘
/*
__authon__ = 'xiaofeizhang'
函数作用:阶乘
*/
#include <stdio.h>
int main(){
long f(int n);
long s;
s = f(2)+f(3)+f(1);
printf("2!+3!+1的结果为:%d\n", s);
return 0;
}
long f(int n){
long t;
int i;
for(i=1, t=1; i<=n; i++)
t *=i;
return(t);
}
/*
2!+3!+1的结果为:9
--------------------------------
Process exited after 0.581 seconds with return value 0
请按任意键继续. . .
*/
递归实现阶乘
/*
__authon__ = 'xiaofeizhang'
函数作用: 嵌套实现阶乘
*/
#include <stdio.h>
int main(void){
long f(int n);
int n;
long result;
printf("请输入值:");
scanf("%d", &n);
result = f(n);
printf("%d的阶乘为:%d", n, result);
return 0;
}
long f(int n){
long t=1;
if (n<0){
printf(" data error");
}
if (n==1 || n==0){
t = 1;
}
else {
t = f(n-1)*n;
}
return t;
}
/*
请输入值:5
5的阶乘为:120
--------------------------------
Process exited after 3.417 seconds with return value 0
请按任意键继续. . .
*/
计算阶乘后相加
/*
__author__ = 'xiaofeizhang'
实现两个数各自求阶乘后相加
*/
#include <stdio.h>
int main(void){
int a, b;
printf("请输入a和b的值:");
scanf("%d, %d", &a, &b);
long add(int x, int y);
add(a, b);
return 0;
}
long add(int x, int y){
long sum=0, x1, y1;
int i;
long f(int x);
sum = f(x) + f(y);
printf("结果为:%d\n", sum);
return(sum);
}
long f(int x){
long t=1;
for (int i=1; i<=x; i++ ){
t *= i;
}
return t;
}
/*
请输入a和b的值:4,5
结果为:144
--------------------------------
Process exited after 5.624 seconds with return value 0
请按任意键继续. . .
*/
汉塔诺
/*
__author__ = 'xiaofeizhang'
汉塔诺
*/
#include <stdio.h>
int main(void){
void hanoi(int disks, char A, char B, char C);
int disks;
printf("输入盘子的数量:");
scanf("%d", &disks);
hanoi(disks, 'A', 'B', 'C');
return 0;
}
void hanoi(int disks, char A, char B, char C){
if (disks == 1){
printf("%c->%c\n", A, B);
}
else{
hanoi(disks-1, A, C, B);
printf("%c->%c\n", A, B);
hanoi(disks-1, C, B, A);
}
}
/*
输入盘子的数量:4
A->C
A->B
C->B
A->C
B->A
B->C
A->C
A->B
C->B
C->A
B->A
C->B
A->C
A->B
C->B
--------------------------------
Process exited after 4.363 seconds with return value 0
请按任意键继续. . .
*/
求年龄
/*
__author__ = 'xiaofeizhang'
函数作用:求年龄
*/
#include <stdio.h>
//例2:5个学生坐在一起,上一个学生比下一个学生分别小2岁,已知第1个学生今年10岁,那么求第5个学生的年龄。
int age(int n)
{
int c;
if(n==1)
c=10;
else
c=age(n-1)+2;
return c;
}
int main()
{
int s;
s = age(5)+age(5)+age(5);
printf("第5个学生的年龄是%d",s);
return 0;
}
/*
第5个学生的年龄是54
--------------------------------
Process exited after 0.02316 seconds with return value 0
请按任意键继续. . .
*/
自定义两个数比大小
/*
__author__ = 'xiaofeizhang'
函数作用:自定义两个数比大小
*/
#include <stdio.h>
int main(void){
void sort(int x, int y);
int a, b;
printf("请输入a和b的值");
scanf("%d, %d", &a, &b);
sort(a, b);
return 0;
}
void sort(int x, int y){
int t;
if (x<y){
t = x;
x = y;
y = t;
}
printf("max为%d, min的为%d\n", x, y);
}
/*
请输入a和b的值12,13
max为13, min的为12
--------------------------------
Process exited after 5.564 seconds with return value 0
请按任意键继续. . .
*/
网友评论