课后作业一:使用计算机计算组合数

设计思想:

import java.util.Scanner;

public class ZuHeShu {

    int a;

    int Jiecheng(int a)

    {求一个数的阶乘}

    int n,k;

    int Qiuzhi(int n,int k)

{使用递归的方法用组合数递推公式计算数的值}

    public static void main(String[] args) {

//实现求阶乘的值

        System.exit(0);//退出  

    }

}

程序流程图:

 

源程序代码:

//20163610 信1603 刘畅

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

import java.util.Scanner;

public class ZuHeShu {

    int a;

    int Jiecheng(int a)//计算阶乘的方法

    {

        int s=1;

        for(int i=a;i>0;i--)

        {

            s=s*i;

        }

        return s;

    }

    int n,k;

    int Qiuzhi(int n,int k)//求杨辉三角求组合数值的方法

    {

        int e;

        if(k==1||n==k)

        {

            e=1;

        }

        else

            {

            e=Jiecheng(Qiuzhi(n-1,k-1))+Jiecheng(Qiuzhi(n-1,k));

            }

        return e;

    }

    public static void main(String[] args) {      

        Scanner sc=new Scanner(System.in);

        ZuHeShu z1=new ZuHeShu();     

 

        System.out.println("请输入要求的行数:");   //输入行数

        int f=sc.nextInt();

        System.out.println("请输入要求的列数:");   //输入列数

        int g=sc.nextInt();

        System.out.println(z1.Qiuzhi(f, g));       //输出值

        System.exit(0);                          //退出 

    }

}

 

结果截图:

 

课后作业二:递归编程解决汉诺塔问题

设计思想:

public class HanNuoTa {

    public static void move() {

        //实现圆盘的移动

    }

    public static void main(String[] args) {

        move();

    }

}

程序流程图:

 

源程序代码:

public class HanNuoTa {

    public static void move(int level, char from, char inter, char to) {//移动圆盘方法

        if (level == 1) {

            System.out.println("from: " + from + "   1 号    to: " + to);

        } else {

            move(level - 1, from, to, inter);

            System.out.println("from:  " + from + " " + level + " 号   to: " + to);

            move(level - 1, inter, from, to);

        }

    }

    public static void main(String[] args) {

        int nDisks = 4;                         //圆盘数

        move(nDisks, 'A', 'B', 'C');            //移动

    }

}

结果截图:

 

课后作业三:使用递归方式判断某个字串是否是回文

设计思想:

import java.util.Scanner;

public class HuiWenShu {                                                  

 

    public static boolean isPalindrome(){

     判断是否回文数

    }

    public static void main(String[] args){

        String s = sc.next();                                                         

  if(是回文数)                              

        {

        System.out.println("是回文数");

        }

        if(不是回文数)                                   

  {

        System.out.println(s+"不是回文数");                          

        }

    } 

}

程序流程图:

源程序代码:

//20163610 信1603 刘畅

import java.util.Scanner;

public class HuiWenShu {                                                    //判断是否回文数

    public static boolean isPalindrome(String s,int a,int b){

        if(a > b)

            throw new IllegalArgumentException();

        if(a == b)

            return true;

        else{

            return (s.charAt(a) == s.charAt(b)) && isPalindrome(s,a+1,b-1);

        }

    }

    public static void main(String[] args){

        Scanner sc=new Scanner(System.in);

        String s = sc.next();                                                   //s存储字符串

        int a = 0;

        int b = s.length() - 1;

        if(HuiWenShu .isPalindrome(s, a, b)==true)                               //s是回文数时输出"是回文数"

        {

        System.out.println("是回文数");

        }

        if(HuiWenShu .isPalindrome(s, a, b)==false)                               //s不是回文数时输出"不是回文数"

        {

        System.out.println(s+"不是回文数");                           

        }

    } 

}

结果截图:

posted on 2017-10-14 00:19  早风  阅读(167)  评论(0编辑  收藏  举报