Programming Assignment 6: Recursion

Here is the original link

  • TrinomialBrute.java (组合数学中的三项式系数,暴力递归法)
public class TrinomialBrute {

    // Returns the trinomial coefficient T(n, k).
    public static long trinomial(int n, int k)
    {
        if (n == 0 && k == 0)
        {
            return 1;
        }
        if (k < -n || k > n)
        {
            return 0;
        }
        return trinomial(n-1, k-1) + trinomial(n-1, k) + trinomial(n-1, k+1);
    }

    // Takes two integer command-line arguments n and k and prints T(n, k).
    public static void main(String[] args)
    {
        int n = Integer.parseInt(args[0]);
        int k = Integer.parseInt(args[1]);
        StdOut.println(trinomial(n, k));
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                StdOut.print(trinomial(i, j) + " ");
            }
            StdOut.println();
        }
    }
}
  • TrinomialDP.java (动态规划求解三项式系数)
public class TrinomialDP {

    // Returns the trinomial coefficient T(n, k).
    public static long trinomial(int n, int k)
    {
        if (n == 0 && k == 0)
        {
            return 1;
        }
        if (k < -n || k > n)
        {
            return 0;
        }
        // 原始数组
        long[] arr = new long[2*n + 1];
        // 辅助数组
        long[] aux = new long[2*n + 1];

        // n表示原始最中间的位置
        arr[n] = 1;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 0; j <= 2*n; j++)
            {
                aux[j] = arr[j];
            }
            aux[n] = arr[n - 1] + arr[n] + arr[n + 1];
            for (int j = 1; j <= i; j++)
            {
                // 防止越界
                if (n - j - 1 < 0)
                {
                    aux[n - j] = 0;
                }
                else
                {
                    aux[n - j] = arr[n - j - 1];
                }
                aux[n - j] += arr[n - j] + arr[n - j + 1];
            }
            for (int j = 1; j <= i; j++)
            {
                // 防止越界
                if (n - j - 1 < 0)
                {
                    aux[n + j] = 0;
                }
                else
                {
                    aux[n + j] = arr[n + j + 1];
                }
                aux[n + j] += arr[n + j - 1] + arr[n + j];
            }
            // 恢复为原来的数组
            for (int j = 0; j <= 2*n; j++)
            {
                arr[j] = aux[j];
            }
        }
        return arr[n + k];
    }

    // Takes two integer command-line arguments n and k and prints T(n, k).
    public static void main(String[] args)
    {
        int n = Integer.parseInt(args[0]);
        int k = Integer.parseInt(args[1]);
        StdOut.println(trinomial(n, k));
    }
}
  • RevesPuzzle.java(汉诺塔问题的升级版,4根柱子的情况)
public class RevesPuzzle {

	// 简单的汉诺塔
    private static void hanoi(int n, int count, char from, char temp, char to)
    {
        if (count == 0)
        {
            return;
        }
        hanoi(n - 1, count - 1, from, to, temp);
        StdOut.println("Move disc " + n + " from " + from + " to " + to);
        hanoi(n - 1, count - 1, temp, from, to);
    }

    private static void reve(int n, char from, char pole1, char to, char pole2)
    {
        if (n == 0)
        {
            return;
        }

        // let k denote the integer nearest to this
        int k = (int)Math.round(n + 1 - Math.sqrt(2*n + 1));
        
        // Transfer (recursively) the k smallest discs to a single pole other than the start or toination poles.
        reve(k, from, to, pole1, pole2);

        // Transfer the remaining n−k disks to the toination pole (without
        // using the pole that now contains the smallest k discs).
        // To do so, use the algorithm for the 3-pole towers of Hanoi problem.
        hanoi(n, n-k, from, pole2, to);

        // Transfer (recursively) the k smallest discs to the toination pole.
        reve(k, pole1, from, to, pole2);
    }

    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        reve(n, 'A', 'B', 'D', 'C');
    }
}
  • A、B、C、D四根柱子,最后可以得到如下的解决方案:
  • RecursiveSquares.java (用递归的方法绘制图案,看起来很有意思)
import java.awt.Color;

public class RecursiveSquares {

    // Draws a square centered on (x, y) of the given side length
    // with a light gray background and a black border.
    public static void drawSquare(double x, double y, double length)
    {
        StdDraw.setPenColor(Color.LIGHT_GRAY);
        StdDraw.filledSquare(x, y, length/2);
        StdDraw.setPenColor(Color.black);
        StdDraw.square(x, y, length/2);
    }

    // Draws a recursive square pattern of order n, centered on (x, y)
    // of the given side length.
    public static void draw(int n, double x, double y, double length)
    {
        if (n == 0) return;
        draw(n - 1, x - length/2, y + length/2, length/2);
        draw(n - 1, x + length/2, y + length/2, length/2);
        drawSquare(x, y, length);
        draw(n - 1, x - length/2, y - length/2, length/2);
        draw(n - 1, x + length/2, y - length/2, length/2);
    }

    // Takes an integer command-line argument n and draws a recursive
    // square pattern of order n, centered on (0.5, 0.5) with side length 0.5.
    public static void main(String[] args)
    {
        int n = Integer.parseInt(args[0]);
        draw(n, 0.5, 0.5, 0.5);
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值