zoukankan      html  css  js  c++  java
  • JAVA课堂动手动脑实验--方法的重载定义,组合数的递归算法

    1.

    请看以下代码,你发现了有什么特殊之处吗?

     

    答:此程序中的两个方法虽然方法名一样,但是参数的数据类型不同;

    这是方法的重载,方法的重载需要满足的条件:

    1)方法名相同;

    2)参数类型不同,参数个数不同,参数类型的顺序不同;

    注意:方法的返回值类型不能作为方法重载的的判断条件。

    2.组合数

    import java.util.Scanner;

    public class Zuheshu {

            public static void main(String[] args){

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

            Scanner sc=new Scanner(System.in);

            int n=sc.nextInt();

            int k=sc.nextInt();

            int result=zuheshu(n,k);

            System.out.println("组合数的结果是 "+result);

            sc.close();

    }

    public static int jiecheng(int n){

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

                     return 1;

               else

                     return jiecheng(n-1)*n;

    }

    public static int zuheshu(int x,int y){

                      int a=jiecheng(x);

                      int b=jiecheng(y);

                      int c=jiecheng(x-y);

                      return a/(b*c);

    }

    }

    杨辉三角

    源代码:

    import java.util.Scanner;

    public class YanghuiZuheshu {

                public static void main(String[] args){

                int i,j,k,n;

                System.out.println("请输入总个数和取得个数:");

                Scanner sc=new Scanner(System.in);

                n=sc.nextInt();

                k=sc.nextInt();

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

                a[0][0]=1;

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

               {

                a[i][0]=a[i][i]=1;

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

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

               }

               System.out.println("一共有"+a[n][k]+"种取法");

    }

     

  • 相关阅读:
    [转]读懂概率图模型:从基本概念和参数估计开始
    [转]ResNet讲解
    [转] GPT-2通俗详解
    Linux/IO基础
    Https:深入浅出HTTPS的交互过程
    Poll/Select/Epoll
    IO相关知识
    认真分析mmap:是什么 为什么 怎么用【转】
    页面滚动时导航渐变
    SSH Secure Shell Client的傻瓜式使用方法
  • 原文地址:https://www.cnblogs.com/Zhanghaonihao/p/5970565.html
Copyright © 2011-2022 走看看