课程作业:将课程作业01、02、03的设计思想、程序流程图、源程序代码和结果截图整理成一篇博文,发表到你的博客园。...

课程作业01

课后作业1_1:

设计思想:输入C(n,k)中的n,k值,利用n!,采用组合数公式直接进行计算方式求得组合数。

程序流程图:

   

源程序代码:

 1 package zuheshu;
 2 import java.math.BigInteger;
 3 import java.util.Scanner;
 4 public class Zuheshu
 5 {
 6     public static void main(String[] args)
 7     {
 8         Scanner con=new Scanner(System.in);
 9         System.out.println("请输入组合数中的n值和k值:");
10         int a=con.nextInt();
11         int b=con.nextInt();
12         if(a<b)
13         {
14             System.out.println("不符合要求,请重新输入:");
15              a=con.nextInt();
16              b=con.nextInt();
17         }
18         System.out.println("n和k构成的组合数值C(n,k)为:"+ca(a).divide(ca(b)).divide(ca(a-b)));
19     }
20     public static BigInteger ca(int n)
21     {
22         if(n==1 || n==0)
23        {
24             return BigInteger.valueOf(1);
25         }
26           return BigInteger.valueOf(n).multiply(ca((n-1)));
27     }
28 }    

结果截图:

   

课后作业1_2:

设计思想:使用递推的方法,先两两叠加求得n+1层的杨辉三角存入二维数组中,再取n+1层的k+1项数值即为要求的C(n,k)值。

程序流程图:

   

源程序代码:

 1 package zuheshu;
 2 import java.util.Scanner;
 3 public class Zuheshu2
 4 {
 5     public static void main(String[] args)
 6     {
 7         int[][] n=new int[20][20];
 8         Scanner con=new Scanner(System.in);
 9         System.out.println("请输入组合数中的n值和k值:");
10         int a=con.nextInt();
11         int b=con.nextInt();
12         if(a<b)
13         {
14             System.out.println("不符合要求,请重新输入:");
15              a=con.nextInt();
16              b=con.nextInt();
17         }
18         for(int h=0;h<a+1;++h)
19             for(int w=0;w<=h;++w)
20             {
21                 if(h==w||w==0)
22                     n[h][w]=1;
23                 else
24                     n[h][w]=n[h-1][w]+n[h-1][w-1];
25             }
26         System.out.println("n和k构成的组合数值C(n,k)为:"+n[a][b]);
27     }
28 }    

结果截图:

   

课后作业1_3:

设计思路:输入C(n,k)中的n和k,使用递归的方法用组合数递推公式计算求值。

程序流程图:

   

源程序代码:

 1 package zuheshu;
 2 import java.util.Scanner;
 3 public class Zuheshu3
 4 {
 5    public static void main(String[] args)
 6    {
 7         Scanner con=new Scanner(System.in);
 8         System.out.println("请输入组合数中的n值和k值:");
 9         int a=con.nextInt();
10         int b=con.nextInt();
11         if(digui(a,b)==0)
12         {
13             System.out.println("不符合要求,请重新输入:");
14              a=con.nextInt();
15              b=con.nextInt();
16         }
17         System.out.println("n和k构成的组合数值C(n,k)为:"+digui(a,b));
18    }
19    public static int digui(int x,int y)
20    {
21        if(x<y)
22            return 0;
23        if(x==y)
24            return 1;
25        if(y==1)
26            return x;
27            return digui(x-1,y-1)+digui(x-1,y);
28    }
29 }

结果截图:

   

课后作业2:

设计思路:采用递归算法进行汉诺塔每层的移动,轮流使用目标塔和初始塔作为辅助塔,完成所有塔层的位移。

程序流程图:

   

源程序代码:

 1 package hannuota;
 2 import java.util.Scanner;
 3 public class Hannuota
 4 {
 5     static int t=1;
 6     public static void main(String[] args)
 7     {
 8         Scanner con=new Scanner(System.in);
 9         System.out.println("请输入汉诺塔层数:");
10         int g=con.nextInt();
11         System.out.println("汉诺塔移动步骤为:");
12         hannuota(g,"A","B","C");
13     }
14     public static void hannuota(int n,String ta1,String ta2,String ta3)
15     {
16         if(n==1)
17             move(ta1,ta3);
18         else
19         {
20             hannuota(n-1,ta1,ta3,ta2);
21             move(ta1,ta3);
22             hannuota(n-1,ta2,ta1,ta3);
23         }
24     }
25     public static void move(String x,String y)
26     {
27         System.out.println("第"+t+"步为:"+x+" -->> "+y);
28         t+=1;
29     }
30 }

结果截图:

   

 

课后作业3:

设计思路:输入字符串后,调用toCharArray函数将字符串转化成字符数组,然后递归对比首尾字符并向内两两缩进,比较次数超过字符数组长度一半就跳出,说明比较完毕,则输出是回文字符串,中途若有判断为不是的,则返回false。

程序流程图:

   

源程序代码:

 1 package huiwen;
 2 import java.util.Scanner;
 3 public class Huiwen
 4 {
 5     static char[] c;
 6     public static void main(String[] args)
 7     {
 8         Scanner con=new Scanner(System.in);
 9         String s=con.next();
10         c=s.toCharArray();
11        if(panduan(0)==true)
12            System.out.println("该字符串是回文字符串");
13        else
14            System.out.println("该字符串不是回文字符串");
15     }
16     public static boolean panduan(int n)
17     {
18         if(n>=c.length/2)  
19             return true;
20         else if(c[n]==c[c.length-1-n])  
21             return panduan(n+1);
22         else return false;
23     }
24 }

结果截图:

   

   

转载于:https://www.cnblogs.com/guobin-/p/7662073.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值