zoukankan      html  css  js  c++  java
  • java

    课后作业

    1. 使用组合数公式利用n!来计算

    (1)设计思想

         主程序中首先键入组合数的两个参数,之后调用方法s,此方法是根据组合数公式编写的,之后此方法中将值返回到主函数中。

    (2)程序流程图

     

    (3)源程序代码

    import java.util.Scanner;

    public class Test1{

    public static void main(String[] args) {

    System.out.println("请输入组合数的参数nk");

    Scanner in=new Scanner(System.in);

    int n=in.nextInt();

    Scanner in1=new Scanner(System.in);

    int k=in1.nextInt();

    in.close();

    in1.close();

    int s=jc(n)/(jc(k)*jc(n-k));

    System.out.println("C("+n+","+k+")="+s);

    }

    public static int jc(int p)

    {

    int a=1;

    if(p<0)

    System.out.println("ERROR");

    else if(p==0||p==1)

    a=1;

    else

    a=jc(p-1)*p;

    return a;

    }

    }

    (4)结果截图

    2.使用递推的方法用杨辉三角形计算

    1)设计思想

    2)程序流程图

    (3)源程序代码

    public static int yanghui(int n,int m) {

        int[][]  arr= new int[n+1][n+1];

        for(int i=0;i<n+1;i++)

        {

         for(int j=i+1;j<n+1;j++)

         {

         arr[i][j]=0;

         }

         for(int j=0;j<i;j++)

         {

         if(j==0)

         {

         arr[i][j]=1;

         }

         else

         {

         arr[i][j]=arr[i-1][j-1]+arr[i-1][j];

         }

         }

        }

        return arr[n][m];

    }

    (4)结果截图

    3.使用递归的方法用组合数递推公式计算

    1)设计思想

       键盘键入组合数的两个参数,编写一个方法,供主函数调用,此方法主要采用递归,。

    (2)程序流程图

     

    3)源程序代码

    import java.util.Scanner;

    public class Test2{

    public static void main(String[] args) {

    System.out.println("请输入组合数的参数n,k:");

    Scanner in=new Scanner(System.in);

    int n=in.nextInt();

    Scanner in1=new Scanner(System.in);

    int k=in1.nextInt();

    in.close();

    in1.close();

    System.out.println("C("+n+","+k+")="+zuhe(n,k));

    }

    public static int zuhe(int a , int b )

    {

    if(a<b||a<0||b<0)

    return 0;   //不合法

    if(a==b)

        return 1;

    if(b==1)

    return a;

    else

    return zuhe(a-1,b)+zuhe(a-1,b-1);

    }

    }

    (4)结果截图

    课后作业

    递归编程解决汉诺塔问题。用Java实现

    (1)设计思想

         定义三个底座分别为A,B,C主要采用递归思想

    (2)程序流程图

     

    3)源程序代码

    import java.io.*;

    public class Test{

    public static void main(String args[]) throws Exception{

    int n;

    System.out.println("请输入盘数:");

    BufferedReader reader=new BufferedReader(

    new InputStreamReader(System.in));

    n=Integer.parseInt(reader.readLine());

    System.out.println("移动方法为:");

    Test hanoi=new Test();

    hanoi.move(n,'A','B','C');//底座为别为A,B,C

    }

    public void move(int n,char a,char b,char c) {//定义底座

    if(n==1)

    System.out.println("盘"+n+":"+a+"-->"+c);

    else

    {

    move(n-1,a,c,b);

    System.out.println("盘"+n+":"+a+"-->"+c);

    move(n-1,b,a,c);

    }

    }

    }

    (4)结果截图

    课后作业三

    (1)设计思想

    程序主要依靠主程序调用“huiwen”这个方法来判断是否为回文数,思想主要是运用了递归的方法。

    (2)程序流程图

     

    3)源程序代码

    import java.util.Scanner;

    public class Test4 {

    public static void main(String[] args) {

    System.out.println("请输入一个字符串:");

    Scanner in =new Scanner(System.in);

    String str=in.nextLine();

    if(huiwen(str)==1)

    System.out.println(str+"是回文字符串。");

    else

    System.out.println(str+"不是回文字符串。");

    in.close();

    }  

    public static int huiwen(String s)

    {

    int length=s.length();

    if(length==0||length==1)

    return 1;

    char head=s.charAt(0);

    char tail=s.charAt(length-1);

    if(head!=tail)

    return 0;

    return huiwen(s.substring(1,length-1));

    }

    }

    (4)结果截图

  • 相关阅读:
    Linux 6 修改ssh默认远程端口号
    win7安装镜像注入USB3.0,NVMe驱动
    Spring Cloud(7):Zuul自定义过滤器和接口限流
    Spring Cloud(6):Zuul的基本使用
    Spring Cloud(5):Hystrix的使用
    Spring Cloud(4):Feign的使用
    Spring Cloud(3):Ribbon的使用
    Spring Cloud(2):搭建Eureka
    Spring Cloud(1):微服务简介
    SpringBoot 2.x (15):Actuator监控
  • 原文地址:https://www.cnblogs.com/woshitiancai/p/7661090.html
Copyright © 2011-2022 走看看