迭代和JDB

迭代和JDB


  • 使用C(n,m)=C(n-1,m-1)+C(n-1,m)公式进行递归编程实现求组合数C(m,n)的功能。
    • 源代码
    public class Combination { public static void main(String args[]) { int c,n,m; n = Integer.parseInt(args[0]); m = Integer.parseInt(args[1]); c = Result(n, m); if (c == -1) System.out.println("Input error: m cannot be larger than n !"); else if (c == -2)System.out.println("Input error: m and n cannot be less than zero !"); else System.out.println(c); } public static int Result(int n, int m) { if (m == 1) return n; else if (m == 0 || m == n) return 1; else if (n < m && n>0 && m>0) return -1; else if (n < 0 || m < 0) return -2; else return Result(n - 1, m - 1) + Result(n - 1, m); }}
    • 测试运行截图
      • 正常情况


- 异常情况


- 边界情况


  • JDB调试
    • 正常情况下用JDB调试程序c(X,2),X为学号最后一位+3

      我学号5214,即取c(7,2)调试。

    • 调试情况截图



相关文章