作业10.13

  1. 使用计算机计算组合数

1使用组合数利用N!来计算:

  1. 设计思想:

程序流程图:首先求出n的阶乘,然后根据组合数公式即可求得

  1. 程序流程图:

 

          

  1. 源程序代码:

package zuheshu;

import java.util.Scanner;

public class Zuheshu {  

public static void main(String args[])

{

@SuppressWarnings("resource")

Scanner in=new Scanner(System.in);

int n,k;

 System.out.println("输入组合数公式的nk");

 n=in.nextInt();

 k=in.nextInt();

  System.out.println("输入错误,请重新输入");

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

n=in.nextInt();

   k=in.nextInt();

     

long C;

     C=calculateN(n)/(calculateN(k)*calculateN(n-k));

     System.out.println("结果为"+C);

}

public static long calculateN(int n)//计算n!的递归公式

{

     if(n==1 || n==0){

            return 1;

     }

            return n*calculateN(n-1);

}

}

  1. 程序截图:

 

1.设计思想:

程序流程图:首先理解杨辉三角,其次做出二维数组,行和列,然后根据组合数公式即可求得

2.程序流程图:

 

3. 源程序代码:

package zuheshu;

import java.util.Scanner;

public class yanghui {

public static void yh(String[] args) {

// TODO 自动生成的方法存根

 Scanner input=new Scanner(System.in);

int n,k;

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

 n=input.nextInt();

 k=input.nextInt();

 int[][] num;             //定义一个int类型的2维数组

 num=new int[20][20];

 for(int m=0;m<20;m++)//主要是对数组进行赋值

{

for(int i=0;i<=m;i++)//m为层数,i为第几个数

{

if(i==0||m==i)//每一层的开头都是1

{

num[m][i]=1;

}

else

num[m][i]=num[m-1][i-1]+num[m-1][i];//递推

}

}

System.out.println("组合数C "+(20-1)+"^"+k+"的值是"+num[20-1][k]);

}

         

}

  1. 程序截图:

 

  1.   汉诺塔问题:
  2. 设计思想:

一次仅能移动一个盘,且不允许大盘放在小盘上面,所以通过递归来解决此问题先把小盘移动c,通过b盘挪动,递归移动n个圆盘,先移动n-1,在挪第n个。

  1. 程序流程图:

 

3.源程序代码:

package hannuota;

import java.util.Scanner;

public class Hannuota {

  static int k =0;//标记移动次数

    //实现移动的函数

    public static void move(int yp ,char N,char M)//yp是几号圆盘

    {

     System.out.println("" + (++k) +" 次移动 : " +

    " "+ yp+" 号圆盘从 " + N  +" 移到  " + M);

    }

public static void move(int n,char a,char b,char c)

    {

if(n==1)

 Hannuota.move(1, a, c); //n1的时候从a移动到c

else

    {

        Hannuota.move(n-1,a,c,b); //n-1个要从a通过c移到b

        Hannuota.move(n,a,c);

        Hannuota.move(n-1,b,a,c);  //递归

    }

}

 public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        char a = 'a';

        char b = 'b';

        char c = 'c';

        while(true)

        {

         System.out.print("请输入圆盘的个数:");

         int yp = input.nextInt();

       

        

        move(yp,'a','b','c');

} }

}

  1. 程序截图:

3.回文数:

1.设计思想:

先设计一个回文数函数,看n是几,在利用首尾是否相同判断以下,接着按照递归,判断中间的字符。

  1. 程序流程图:

 

  1. 源程序代码:

//1605-3 20163532 马鑫

package huiwenshu;

import java.util.Scanner;

public class Huiwenshu {

    public static boolean huiwen( String s,int i,int len ){

    

     if (i==len)

     return true;

     else{

            return (s.charAt(i) == s.charAt(len)) && huiwen(s,i+1,len-1);

        }

    }

    public static void main(String[] args ){

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

     Scanner in=new Scanner(System.in);

      int len;

      String s = in.next();

      int i = 0;

      int len1 = s.length() - 1;

      System.out.println(s + " 是回文数吗? " + Huiwenshu .huiwen(s, i, len1));

    

    }

}

4.程序截图:

 

原文地址:https://www.cnblogs.com/maxin123/p/7660255.html