迭代和JDB

目录

要求

1 使用C(n,m)=C(n-1,m-1)+C(n-1,m)公式进行递归编程实现求组合数C(m,n)的功能
2 m,n 要通过命令行传入
3 提交测试运行截图(至少三张:正常如c(3,2)、异常如c(2, 3)、边界情况如c(m,m))
4 提交正常情况下用JDB调试程序c(X,2)的截图,X为学号最后一位+3,至少四张截图
5 把代码推送到码云

1. 程序:

public class Recursion {                //执行C(n,m)的递归公式
    int recursion(int n, int m) {
        int nChildren = n-1;
        int[] mChildren = new int[2];
        int result;
        mChildren[0] = m-1;
        mChildren[1] = m;
        if (nChildren < mChildren[1] || nChildren==0 || mChildren[1]==0) {
            return 1;
        }
        result = recursion(nChildren, mChildren[0])+recursion(nChildren, mChildren[1]);
        return result;
    }
}

public class Estimate {             //执行主要的判断
    void estimate(int n, int m) {
        int result;
        if (n<m && n>=0 && m>=0) {
            System.out.println("Error: m is bigger than n!");
        }
        else if (n>=m && n>=0 && m<0) {
            System.out.println("Error: m is smaller than 0!");
        }
        else if (n>=m && n<0 && m<0) {
            System.out.println("Error: both n and m is smaller than 0!");
        }
        else if (n>=m && n>=0 && m>=0) {
            Recursion recu = new Recursion();           //创建 Recursion 类的对象
            result=recu.recursion(n, m);            //执行递归程序,并赋值给 result
            System.out.println(result);             //输出计算结果
        }
        else {
            System.out.println("Error: unknown!");
        }
    }
}

public class Combination {             //主类
    public static void main(String[] args) {
        int n=0, m=0;
        if (args.length == 2) {             //判断命令行输入参数的格式是否正确
            n = Integer.parseInt(args[0]);             //将命令行参数中的 String 型转化为 int 型,并赋值给 n 、 m
            m = Integer.parseInt(args[1]);
        }
        else {
            System.out.println("Input args Error!");
            System.exit(0);
        }
        Estimate esti = new Estimate();
        esti.estimate(n, m);
    }
}

2. 命令行传入

使用命令行传入:java -cp out Combination 5 4 等。

3. C(n,m)编译三种情况:

  • C(n,m)编译正常情况:

image.png

  • C(n,m)编译异常情况:

image.png

  • C(n,m)编译边界情况:

image.png

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

我的学号是5223,取最后一位 3+3=6,所以X=6,命令如下:

jdb -classpath .:./out Combination 6 2

[图片1]

image.png

[图片2]

image.png

[图片3]

image.png
说明:由于 Combination 主类中递归结束,所有 updown 都显示 堆栈结束。

[图片4]

image.png

5. 代码上传

image.png

6. 附:IDEA编写截图

事先将程序在IDEA中编写运行调试完成,再转到虚拟机中。

image.png
image.png
image.png

原文地址:https://www.cnblogs.com/Yogile/p/10597486.html