C语言函数递归练习

递归求Fabonacci数列

本题要求实现求Fabonacci数列项的函数。Fabonacci数列的定义如下:

f(n)=f(n−2)+f(n−1) (n≥2),其中f(0)=0,f(1)=1。

函数接口定义:
int f( int n );
函数f应返回第n个Fabonacci数。题目保证输入输出在长整型范围内。建议用递归实现。

裁判测试程序样例:
#include <stdio.h>

int f( int n );

int main()
{
    int n;
    
    scanf("%d", &n);
    printf("%d\n", f(n));
    
    return 0;
}/* 你的代码将被嵌在这里 */
输入样例:
6
输出样例:
8
代码:

#define  _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

int f(int n)
{
    if (n == 0)
        return 0;
    else if (n == 1)
        return 1;
    else
        return f(n - 2) + f(n - 1);
}

int main()
{
    int n;

    scanf("%d", &n);
    printf("%d\n", f(n));

    return 0;
}


递归求阶乘和


本题要求实现一个计算非负整数阶乘的简单函数,并利用该函数求 1!+2!+3!+...+n! 的值。
函数接口定义:
double fact( int n );
double factsum( int n );
函数fact应返回n的阶乘,建议用递归实现。函数factsum应返回 1!+2!+...+n! 的值。题目保证输入输出在双精度范围内。

裁判测试程序样例:
#include <stdio.h>
double fact( int n );
double factsum( int n );
int main()
{
    int n;

    scanf("%d",&n);
    printf("fact(%d) = %.0f\n", n, fact(n));
    printf("sum = %.0f\n", factsum(n));
        
    return 0;
}/* 你的代码将被嵌在这里 */
输入样例1:
10
输出样例1:
fact(10) = 3628800
sum = 4037913
输入样例2:
0
输出样例2:
fact(0) = 1
sum = 0
代码:

#define  _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

double fact(int n)
{
    int sum = 0;
    if (n == 1||n==0)
        return 1;
    sum = n * (fact(n - 1));
    return sum;
}
double factsum(int n) 
{
    int sum = 0;
    while (n != 0)
    {
        sum = sum + fact(n);
        n--;
    }
    return sum;
}

int main()
{
    int n;

    scanf("%d", &n);
    printf("fact(%d) = %.0f\n", n, fact(n));
    printf("sum = %.0f\n", factsum(n));

    return 0;
}


圆形体体积计算器


本题要求实现一个常用圆形体体积的计算器。计算公式如下:
球体体积 V= 
3
4

 πr 
3
 ,其中r是球体半径。
圆柱体体积 V=πr 
2
 h,其中r是底圆半径,h是高。
圆锥体体积 V= 
3
1

 πr 
2
 h,其中r是底圆半径,h是高。
输入格式:
在每次计算之前,要求输出如下界面:

1-Ball
2-Cylinder
3-Cone
other-Exit
Please enter your command:
然后从标准输入读进一个整数指令。

输出格式:
如果读入的指令是1或2或3,则执行相应的体积计算;如果是其他整数,则程序结束运行。

当输入为1时,在计算球体体积之前,打印Please enter the radius:,然后读入球体半径,完成计算;
当输入为2时,在计算圆柱体体积之前,打印Please enter the radius and the height:,然后读入底圆半径和高,完成计算;
当输入为3时,在计算圆锥体体积之前,打印Please enter the radius and the height:,然后读入底圆半径和高,完成计算。
计算结果在一行内输出,保留小数点后两位。输入样例:
1
2
3
2.4 3
0
输出样例:
1-Ball
2-Cylinder
3-Cone
other-Exit
Please enter your command:
Please enter the radius:
33.51
1-Ball
2-Cylinder
3-Cone
other-Exit
Please enter your command:
Please enter the radius and the height:
18.10
1-Ball
2-Cylinder
3-Cone
other-Exit
Please enter your command:
代码:
 

#define  _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#define PI 3.14159
int main()
{
        int n = 0;
        while (1)
        {
            print_f();
            scanf("%d", &n);
            if (n == 1)
            {
                printf("Please enter the radius:\n");
                ball_1();
            }
            else if (n == 2)
            {
                printf("Please enter the radius and the height:\n");
                cylinder_2();
            }
            else if (n == 3)
            {
                printf("Please enter the radius and the height:\n");
                cone_3();
            }
            else
                break;
        }
        return 0;
}
int  ball_1()
{
    double r = 0,V=0;
    scanf("%lf", &r);
    V = 4.0/3.0 * PI * r * r * r;
    printf("%.2lf\n", V);
    return 0;
}
int cylinder_2()
{
    double r = 0,h=0, V = 0;
    scanf("%lf%lf", &r,&h);
    V = PI * h * r * r;
    printf("%.2lf\n", V);
    return 0;
}
int cone_3()
{
    double r = 0, V = 0,h=0;
    scanf("%lf%lf", &r, &h);
    V = 1.0/3.0 * PI * r * r * h;
    printf("%.2lf\n", V);
    return 0;
}

int print_f()
{
    printf("1-Ball\n2 - Cylinder\n3 - Cone\nother - Exit\nPlease enter your command : \n");
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值