迭代和JDB
目錄
- 要求
- 1. 程序:
- 2. 命令行傳入
- 3. C(n,m)編譯三種情況:
- 4. 正常情況下用JDB調試程序c(X,2),X為學號最后一位+3
- 5. 代碼上傳
- 6. 附:IDEA編寫截圖
目錄
要求
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); //執行遞歸程序,并賦值給 resultSystem.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 、 mm = 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)編譯正常情況:
- C(n,m)編譯異常情況:
- C(n,m)編譯邊界情況:
4. 正常情況下用JDB調試程序c(X,2),X為學號最后一位+3
我的學號是5223,取最后一位 3+3=6,所以X=6,命令如下:
jdb -classpath .:./out Combination 6 2[圖片1]
[圖片2]
[圖片3]
說明:由于 Combination 主類中遞歸結束,所有 up 或 down 都顯示 堆棧結束。
[圖片4]
5. 代碼上傳
6. 附:IDEA編寫截圖
事先將程序在IDEA中編寫運行調試完成,再轉到虛擬機中。
轉載于:https://www.cnblogs.com/Yogile/p/10597486.html
總結
- 上一篇: NOIP 2006 T2 金明的预算方案
- 下一篇: oppo设备怎么样无需root激活XPO