publicclassTrinomialBrute{// Returns the trinomial coefficient T(n, k).publicstaticlongtrinomial(int n,int k){if(n ==0&& k ==0){return1;}if(k <-n || k > n){return0;}returntrinomial(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).publicstaticvoidmain(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 (动态规划求解三项式系数)
publicclassTrinomialDP{// Returns the trinomial coefficient T(n, k).publicstaticlongtrinomial(int n,int k){if(n ==0&& k ==0){return1;}if(k <-n || k > n){return0;}// 原始数组long[] arr =newlong[2*n +1];// 辅助数组long[] aux =newlong[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).publicstaticvoidmain(String[] args){int n =Integer.parseInt(args[0]);int k =Integer.parseInt(args[1]);StdOut.println(trinomial(n, k));}}
RevesPuzzle.java(汉诺塔问题的升级版,4根柱子的情况)
publicclassRevesPuzzle{// 简单的汉诺塔privatestaticvoidhanoi(int n,int count,char from,char temp,charto){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);}privatestaticvoidreve(int n,char from,char pole1,charto,char pole2){if(n ==0){return;}// let k denote the integer nearest to thisint 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);}publicstaticvoidmain(String[] args){int n =Integer.parseInt(args[0]);reve(n,'A','B','D','C');}}
A、B、C、D四根柱子,最后可以得到如下的解决方案:
RecursiveSquares.java (用递归的方法绘制图案,看起来很有意思)
importjava.awt.Color;publicclassRecursiveSquares{// Draws a square centered on (x, y) of the given side length// with a light gray background and a black border.publicstaticvoiddrawSquare(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.publicstaticvoiddraw(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.publicstaticvoidmain(String[] args){int n =Integer.parseInt(args[0]);draw(n,0.5,0.5,0.5);}}
Here is the original linkTrinomialBrute.java (组合数学中的三项式系数,暴力递归法)public class TrinomialBrute { // Returns the trinomial coefficient T(n, k). public static long trinomial(int n, int k) { if (n == 0 && k == 0) {