递归算法

1.一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。
答:public class MainClass
{
public static void Main()
{
Console.WriteLine(Foo(30));
}
public static int Foo(int i)
{
if (i <= 0)
return 0;
else if(i > 0 && i <= 2)
return 1;
else return Foo(i -1) + Foo(i - 2);
}
}

以上是C#的代码,下面是asp.net(vb.net代码)

    Function FunFoo(ByVal V As Integer) As Long
        If (V <= 0) Then
            FunFoo = 0
        ElseIf (V > 0 And V <= 2) Then
            FunFoo = 1
        Else
            FunFoo = FunFoo(V - 1) + FunFoo(V - 2)
        End If
    End Function

 

 

2. 求5的阶乘。。(java写的代码)  
public class Test { 
static int multiply(int n){ 
if(n==1||n==0) 
return n; 
else 
return n*multiply(n-1); 

public static void main(String[] args){ 
System.out.println(multiply(10)); 

}

 

 

3.(转帖)

情景是:一元一瓶的汽水,2个空瓶能换一瓶汽水。
问假如你有X元钱,能喝Y瓶汽水. 用y=f(x)表示
如果是N空瓶换一个空瓶呢,用y=f(x,n)表示

我的代码,如果是2换1:
package lihan;
public class test {
    public static Integer digui(int x)
    {
        if(x==1)
        {
            return x;
        }
        if(x%2==1)
        return x+digui(x/2)+1;
        else
            return x+digui(x/2);
            }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO 自动生成方法存根
        int i=digui(11);
        System.out.print(i);

    }

}

如果是N换1的话,那么
public class lihan {
    public static Integer digui(int x,int n)
    {
        if(x==1)
        {
            return x;
        }
        if(x%n==(n-1))
        return x+digui(x/n,n)+1;
        else
            return x+digui(x/n,n);
            }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO 自动生成方法存根
        int i=digui(12,3)+1;
        System.out.print(i);
    }

}

 

4.java冒泡排序算法(转帖) 

package lihan;

public class wordTest
{
    public static void main(String args[])
    {
        int[] m =
        { 2, 8, 43, 3, 33 };
        int[] n = sort(m);
        for (int i = 0; i < m.length; i++)
        {
            System.out.println(n[i] + "/n");
        }

    }

    /* 冒泡排序算法 */
    public static int[] sort(int[] m)
    {
        int intLenth = m.length;
        /*执行intLenth次*/
        for (int i = 0; i < intLenth; i++)
        {
            /*每执行一次,将最小的数排在后面*/
            for (int j = 0; j < intLenth - i - 1; j++)
            {
                int a = m[j];
                int b = m[j + 1];
                if (a < b)
                {
                    m[j] = b;
                    m[j + 1] = a;
                }
            }
        }
        return m;

    }
}

编程实现一个冒泡排序算法?(C#程序代码)
int [] array = new int ;
int temp = 0 ;
for (int i = 0 ; i < array.Length - 1 ; i++)
{
for (int j = i + 1 ; j < array.Length ; j++)
{
if (array[j] < array[i])
{
temp = array[i] ;
array[i] = array[j] ;
array[j] = temp ;
}
}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值