[算法导论]小递归

#include<stdio.h>

void Reverse();
int aM1(int x);
int aM2(int x);
float bM(int n, float x);
float cM1(int n, int x);
float cM2(int n, int x);

int main ()
{
    printf("以'.'为终止 倒叙打印\n");
   // Reverse();
    /*定义 a函数
        M(X)= x-10         x>100
              M(M(X+11))  x<=100
    */
    printf("\na函数为:%d\n", aM1(88));
    printf("a函数为:%d\n", aM2(88));

    /*定义 b函数
        Mn(X)= 1                       n=0
              2x                       n=1
              2xMn-1(X)-2(n-1)Mn-2(X)  n>1
    */
    printf("Mn(3)的值是%f", bM(4,2));
    printf("%f", cM1(4, 7));
    printf("%f", cM2(4, 7));

}

void Reverse()
{
    char ch;
    scanf("%c", &ch);
    if(ch!='.')
    {
        Reverse();
        printf("%c\t", ch);
    }
}

int aM1(int x)//函数1递归
{
    int y;
    if(x>100)
        return x-10;
    else
    {
        y=aM1(x+11);
        return aM1(y);
    }
}

int aM2(int x)//函数1非递归
{
    int y,  level=0;
    if(x>100)
        return x-10;

    while(level>=0)
    {
        if(y>100)
        {
            level--;
            y=y-10;
        }
        else
        {
            level++;
            y=y+11;
        }
    }
    return y;
}

float bM(int n, float x)
{
    float s;
    if(n==0)
        return 1;
    else
    {
        if(n==1)
            return 2*x;
        else
        {
            s=2*x*bM(n-1, x)-2*(n-1)*bM(n-2, x);
            return s;
        }
    }
}

float cM1(int n, int x)
{
    float s;
    if(n==0)
        return 1;
    else if(n==1)
        return x;
    else
    {
        s=((2*n-1)*x*cM1(n-1, x)-(n-1)*cM1(n-2, x))/n;
        return s;
    }
}

float cM2(int n, int x)
{
    float s, s1, s2;
    int i;
    if(n==0)
        return 1;
    else if(n==1)
        return x;
    else
    {
        s1=1;
        s2=x;
        for(i=2; i<=n; i++)
        {
            s=((2*i-1)*x*s2-(i-1)*s1)/i;
            s1=s2;
            s2=s;
        }
    }
    return s;
}

float dM(float n)
{
    int f;
    if(n==0)
        return 1;
    else
    {
        n=dM(n/2)*n;
        return n;
    }
}

float dM(float n)
{
    int f;
    int s[100][3];
    int top=-1, t;
    top++;
    s[top][1]=n;
    while(n)
    {
       top++;
       n=n/2;
       s[top][1]=n;
    }
    s[top][0]=1;
    while(top>0)
    {
        t=s[top][0];
        top--;
        s[top][2]=t;
        s[top][0]=s[top][1]*s[top][2];
    }
    return s[top][0];
    }
}

void eM(int n, int m)
{
    if(m=0 && n>=0)
        return 1;
    else if(m==n && n>=0)
        return 1;
    else
    {
        return eM(n-1, m)+eM(n-1, m-1);
    }
}

void eM(int n ,int m)
{
    int s[100][3];
    int s1, s2, top=0;
    s[top][0]=0;
    s[top][1]=n;
    s[top][2]=m;
    while(n>m && m>=0 && n>=0)
    {
        if(s[top][0]==0)
        {
            top++;
            s[top][0]=0;
            s[top][1]=s[top-1][1]-1;
            s[top][2]=s[top-1][2];

        if(s[top][2]==0 || s[top][1]==s[top][2])
            s[top][0]=1;
        }
        if(top>=1 && s[top][0]>0 && s[top-1][0]==0)
        {
            top++;
            s[top][0]=0;
            s[top][1]=s[top-2]

        if(s[top][2]==0 || s[top][2]==s[top][2])
            s[top][0]=1;
        }
        if(top>1 && s[top][1]>0 && s[top-1][1]>0)
        {
            s1=s[top][1];
            s2=
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值