码迷,mamicode.com
首页 > 其他好文 > 详细

课后作业

时间:2017-10-13 23:47:24      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:数学   adl   键盘   out   png   inpu   组合数   except   style   

 

课程作业01

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

设计思想:

通过键盘输入k和n的值,利用已经给出的式子进行计算,先算n!,然后计算k!,然后再计算(n-k)!,最后的结果就是n!/k!*(n-k)!,最后输出结果。

程序流程图:

 技术分享

 

源程序代码:

//赵春辉 信1605-3 20163464 杨辉三角,组合数,阶乘(1)

import java.util.Scanner;

public class jiecheng

{

    static jiecheng h=new jiecheng();

    Scanner sc=new Scanner(System.in);

    public void jisuan()

    {

        int k=sc.nextInt();

        int n=sc.nextInt();

        int result1=1;

        int result2=1;

        int result3=1;

        int result;

        if(k>n)

        {

            System.out.println("输入的有误,n需要大于k请重新输入");

            h.jisuan();

        }

        if(k<=n)

        {

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

            {

                result1=result1*i;

            }//n!

            for(int i=1;i<=k;i++)

            {

                result2=result2*i;

            }//k!

            for(int i=1;i<=n-k;i++)

            {

                result3=result3*i;

            }

            result=result1/result2*result3;

                System.out.println("result is"+result);

        }

    }

    public static void main(String args [])

    {

        System.out.println("please enter the k and n:");

        h.jisuan();

    }

}

结果截图:

 技术分享

 

 

 

02设计思想:

运用递推的方法,首先先建立一个函数,计算k,n的结果然后再次引用这个函数计算k,n+1的值,就可以了。

程序流程图:

 技术分享

 

源程序代码://赵春辉 信1605-3 20163464 杨辉三角,组合数,阶乘(1)

import java.util.Scanner;

public class yanghui 

{

    static yanghui h=new yanghui();

    Scanner sc=new Scanner(System.in);

    public void jisuan(int k,int n)

    {

         k=sc.nextInt();

        n=sc.nextInt();

        int result1=1;

        int result2=1;

        int result3=1;

        int result;

        if(k>n)

        {

            System.out.println("输入的有误,n需要大于k请重新输入");

            h.jisuan(k,n);

        }

        if(k<=n)

        {

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

            {

                result1=result1*i;

            }//n!

            for(int i=1;i<=k;i++)

            {

                result2=result2*i;

            }//k!

            for(int i=1;i<=n-k;i++)

            {

                result3=result3*i;

            }

            result=result1/result2*result3;

                System.out.println("result is"+result);

        }

       

    }

    public static void main(String args [])

    {

        System.out.println("please enter the k and n+1:");

        int k = 0;int n=0;

        h.jisuan( k, n);

    }

}

 

结果截图:

 

 

 技术分享

 

 

 

 

 

 

课程作业02

设计思想:

这个问题可用递归法解决,并用数学归纳法又个别得出普遍解法: 假设塔座B上有3个圆盘移动到塔座A上: 

(1)"将塔座B上2个圆盘借助塔座A移动到塔座C上;

(2)"将塔座B上1个圆盘移动到塔座A上; 

(3)"将塔座C上2个圆盘借助塔座B移动到塔座A上。

其中第2步可以直接实现。第1步又可用递归方法分解为:

1.1"将塔座B上1个圆盘从塔座X移动到塔座A;

1.2"将塔座B上1个圆盘从塔座X移动到塔座C;

1.3"将塔座A上1个圆盘从塔座Z移动到塔座C。

第3步可以分解为:

 3.1 将塔座C上1个圆盘从塔座Y移动到塔座B;

3.2 将塔座C上1个圆盘从塔座Y移动到塔座A;

3.3 将塔座B上1个圆盘从塔座X移动到塔座A。

所以可得到移动3个圆盘的步骤为 B->A,B->C, A->C, B->A, C->B, C->A, B->A。

 

程序流程图:

 技术分享

 

 

源程序代码:

//信1605-3  赵春辉  20163464   汉诺塔

import java.util.Scanner;

public class hannuota

{   

    public static void main(String[] args)

    {  Scanner sc=new Scanner(System.in);

    hannuota h = new hannuota(); 

    System.out.println("please enter it‘s tower");

    int n=sc.nextInt();

        h.solve(n, "a", "b", "c"); 

    } 

    public void solve(int n, String a, String b, String c)

    { 

        if(n == 1)

        { 

            move(n, a, c); 

        }

        else

        { 

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

            move(n, a, c); 

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

        } 

    } 

    public void move(int n, String a, String c)

    { 

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

    } 

 

}

 

结果截图:

 技术分享

课程作业03

设计思想:

判断一个字符串是不是回文串,首先要建立数组,将数组的字符串存储在数组中,输入内容以后就要通过循环语句来判断是不是一个回文串,可以用布尔的true和false来判断,在判断的时候就是要判断第一位和最后一位是不是一样,同理要判断第二位和倒数第二位是不是一样,以此类推,一直判断到最后,假如中间判断出不一样以后,直接用break跳出循环,这样就可以判断一个字符串是不是回文串了。

 

流程图:

 

 技术分享

 

 

 

 

 

 

 

 

 

 

                                                           

                                                                                                           

 

                                                                                                                                 

 

源程序代码:

//信1605-3 赵春辉20163464 判断输入的是不是回文串

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class huiwen

{

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

     {

          String str ="";

          BufferedReader br = new BufferedReader(new InputStreamReader(System.in),256);//创建一个数组;

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

          str = br.readLine();//读取输入的数组内容,存储在数组中

          System.out.println("结果: " + huiwen(str,0,str.length()-1));

     }

     public static boolean huiwen(String s,intstart,intend)

     {

 

         if(start == end) return true;

          if(start > end){

           System.out.println("没有字符串!");

           return false;

      }

      if(s.charAt(start) == s.charAt(end))

      {

            return huiwen(s,start+1,end-1);

      }

      else{

           return false;

      }

     }

 

}

 

 

结果截图:

 技术分享

 

                                                                 

课后作业

标签:数学   adl   键盘   out   png   inpu   组合数   except   style   

原文地址:http://www.cnblogs.com/zhaochunhui/p/7663698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!