C语言(二)

这篇博客主要介绍了递归在计算阶乘、指数、交错幂级数部分和、斐波那契数列以及十进制转二进制等数学问题中的应用。此外,还探讨了如何使用递归实现整数按位顺序输出以及在单链表中删除特定值的结点。这些实例展示了递归在解决复杂问题时的简洁性和效率。

1、递归求阶乘和
本题要求实现一个计算非负整数阶乘的简单函数,并利用该函数求 1!+2!+3!+…+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;
}

/* 你的代码将被嵌在这里 */

代码:

double fact( int n )//求阶乘
{
    if (n == 1)
        return 1;
    return n * fact(n - 1);
}
double factsum( int n )
{
    double sum = 0;
    for (int i = 1; i <= n; i++)
        sum + = fact(i);
    return sum;
}

2、递归实现指数函数
本题要求用递归实现一个计算x的n次幂 (n≥1)的函数。

裁判测试程序样例:

#include <stdio.h>

double calc_pow( double x, int n );

int main() {
    double x;
    int n;

    scanf("%lf %d", &x, &n);
    printf("%.0f\n", calc_pow(x, n));

    return 0; }
    /* 你的代码将被嵌在这里 */

代码:

double calc_pow( double x, int n )
{
    if (n == 0)
        return 1;
    return x * calc_pow (x, n - 1);
}

3、递归求简单交错幂级数的部分和
本题要求实现一个函数,计算简单交错幂级数的部分和。

裁判测试程序样例:

#include <stdio.h>

double fn( double x, int n );

int main()
{
    double x;
    int n;
    
    scanf("%lf %d", &x, &n);
    printf("%.2f\n", fn(x,n));
    
    return 0;
}

/* 你的代码将被嵌在这里 */

方法一:

double calc_pow( double x, int n )    //计算x^n
{
    if (n == 0)
        return 1;
    return x * calc_pow (x, n - 1);
}
double fn( double x, int n )
{
    if (n == 1)
        return x;
    return calc_pow (-1, n - 1) * calc_pow (x, n) + fn (x, n - 1);
}

方法二:

double fn(double x, int n)
{
    double result;
      if (n == 0 || n == 1)
        result = x;
    else
        result = x - x * fn(x, n-1);
    return result;
}

方法一采用两种递归函数,是比较容易理解的方式;方法二基于对交错幂级数的部分和式子的观察,得出规律 fn(x, n) = x - x * fn(x, n-1)。

4、递归求Fabonacci数列
本题要求实现求Fabonacci数列项的函数。Fabonacci数列的定义如下:f(n)=f(n−2)+f(n−1) (n≥2),其中f(0)=0,f(1)=1。

裁判测试程序样例:

#include <stdio.h>

int f( int n );

int main()
{
    int n;
    
    scanf("%d", &n);
    printf("%d\n", f(n));
    
    return 0;
}

/* 你的代码将被嵌在这里 */

代码:

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

5、十进制转换二进制
本题要求用递归实现一个函数,将非负整数n转换为二进制后输出。

裁判测试程序样例:

#include <stdio.h>

void dectobin( int n );

int main()
{
    int n;
    
    scanf("%d", &n);
    dectobin(n);
    
    return 0;
}

/* 你的代码将被嵌在这里 */

代码:

void dectobin( int n )
{
	if (n / 2 > 0) 
        dectobin (n / 2);
	printf ("%d", n % 2);
}

ps:通过递归工作栈来实现进制转换。

6、递归实现顺序输出整数
本题要求实现一个函数,对一个整数进行按位顺序输出。

裁判测试程序样例:

#include <stdio.h>

void printdigits( int n );

int main()
{
    int n;
    
    scanf("%d", &n);
    printdigits(n);

    return 0;
}

/* 你的代码将被嵌在这里 */

代码:

void printdigits( int n )
{
    if (n / 10 > 0)
        printdigits(n / 10);
    printf ("%d\n", n % 10);
}

7、单链表结点删除
本题要求实现两个函数,分别将读入的数据存储为单链表、将链表中所有存储了某给定值的结点删除。链表结点定义如下:
struct ListNode {
int data;
ListNode *next;
};
函数接口定义:
struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );
函数readlist从标准输入读入一系列正整数,按照读入顺序建立单链表。当读到−1时表示输入结束,函数应返回指向单链表头结点的指针。
函数deletem将单链表L中所有存储了m的结点删除。返回指向结果链表头结点的指针。

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>

struct ListNode {
    int data;
    struct ListNode *next;
};

struct ListNode *readlist();
struct ListNode *deletem( struct ListNode *L, int m );
void printlist( struct ListNode *L )
{
     struct ListNode *p = L;
     while (p) {
           printf("%d ", p->data);
           p = p->next;
     }
     printf("\n");
}

int main()
{
    int m;
    struct ListNode *L = readlist();
    scanf("%d", &m);
    L = deletem(L, m);
    printlist(L);

    return 0;
}

/* 你的代码将被嵌在这里 */

代码:

struct ListNode* readlist()//尾插法创建链表(不带头结点)
{
    struct ListNode* head = NULL, * tail = NULL, * p = NULL;
    int data;
    scanf("%d", &data);
    while (data != -1)
    {
        p = (struct ListNode*)malloc(sizeof(struct ListNode));
        p->data = data;
        p->next = NULL;
        if (head == NULL)
            head = p;
        else
            tail->next = p;
        tail = p;
        scanf("%d", &data);
    }
    return head;
}

struct stud_node *deletelist( struct stud_node *head, int min_score )
{
	struct stud_node *p1, *p2;
	p1 = NULL;//前一个节点
	p2 = head;//后一个节点
	while(p2!=NULL)//非空表时
	{
		if(p2->score<min_score) 
		{
			if(p1==NULL)//前一节点为空,即当前节点为首节点时
			{
				head = head->next;//直接删头结点
			}
			else
			{
				p1->next = p2->next;
			}
		}
		else //该节点不改删,前一节点往后走
		{
			p1 = p2;
		}
		
		p2 = p2->next;//后节点往后走	
	}
	
	return head;
}

总结:今日进行了递归的练习,以及一道链表结点删除的题,明日计划深入练习数据结构相关题型。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值