首页 > 代码库 > 课后作业2

课后作业2

动手动脑:

1.java类中不用static还能如何调用函数

方法调用:“类名.方法”

如果此函数是动态的,那么只有在调用的时候才会被主动加载一次,之后释放内存。方法调用:必须先实例化出来一个类,之后再通过实例化类名+方法的形式调用。

2.观察以下代码的不同之处

技术分享

表示方法为静态,在其他类中可以用类名去调用这个方法。

课后作业:

1.求组合数源程序:

1)递归求组合数:

import java.util.Scanner; public class Czuhe {    public static void main(String[] args)    {                 System.out.print("请输入N:");        @SuppressWarnings("resource")        Scanner sc=new Scanner(System.in);        int N=sc.nextInt();        System.out.print("请输入k:");        @SuppressWarnings("resource")        Scanner scan=new Scanner(System.in);        int K=scan.nextInt();        System.out.println(Cal(N,K));    }    public static int Cal(int N,int K)    {         if(K == 0 || N == 0)             {                 return 1;             }                            if(N < K)             {                 return 0;             }                            if(N == K)             {                 return 1;             }                            return Cal(N-1,K) + Cal(N-1,K-1);    }}

(2)利用杨辉三角求组合数:

import java.util.Scanner;@SuppressWarnings("unused")public class Czuhe {    public static void main(String[] args)    {                 System.out.print("请输入N:");        @SuppressWarnings("resource")        Scanner sc=new Scanner(System.in);        int N=sc.nextInt();        System.out.print("请输入k:");        @SuppressWarnings("resource")        Scanner scan=new Scanner(System.in);        int K=scan.nextInt();        System.out.println(Cal(N,K));    }    public static int Cal(int N,int K)    {         if(K == 0 || N == 0)             {                 return 1;             }              int c=0;int a=1;         int s=Math.min(K, N-K);         for(int i=1;i<=s;i++)         {             c=a*(N-i+1)/(i);             a=c;         }                           return c;                 }}

(3)使用组合数公式计算:

import java.util.Scanner;@SuppressWarnings("unused")public class Czuhe {    public static void main(String[] args)     {                System.out.print("请输入N:");        @SuppressWarnings("resource")        Scanner sc=new Scanner(System.in);        int N=sc.nextInt();        System.out.print("请输入k:");        @SuppressWarnings("resource")        Scanner scan=new Scanner(System.in);        int K=scan.nextInt();        System.out.println(jiecheng(N)/(jiecheng(K)*jiecheng(N-K)));    }    public static int jiecheng(int N)    {        int s=1;         if(N == 1)              {                  return 1;              }                   else              s=jiecheng(N-1)*N  ;          return s;                 }}

2.回文数程序:

import java.util.Scanner;@SuppressWarnings("unused")public class HuiWen {    int n=100;    public static void main(String[] args)     {        System.out.print("请输入字符串:");        @SuppressWarnings("resource")        Scanner sc=new Scanner(System.in);        String N=sc.nextLine();        if(fun(N)==1)             System.out.print("是回文字符串!");        else              System.out.print("不是回文字符串!");    }            public static  int fun(  String str)    {          int len=str.length();                  {              if (len == 0 || len == 1)                  return    1;              char first=str.charAt(0);            char last=str.charAt(len-1);            if ( first!= last)                  return    0;              return fun(str.substring(1,len-1));          }      }}

3.线性同余法生成随机数

import java.util.Date;  import java.util.concurrent.atomic.AtomicLong;    public class Random {            private final AtomicLong seed;      private final static long multiplier = 0x5DEECE66DL;      private final static long addend = 0xBL;      private final static long mask = (1L << 48) - 1;      private static volatile long seedUniquifier = 8682522807148012L;            public void srandSeed(long t){          this.seed.set(t);      }            public Random(){           this(++seedUniquifier + System.nanoTime());           System.out.println(seed.get());      }            public Random(long seed) {          this.seed = new AtomicLong(0L);          srandSeed(seed);      }            int random(int bits){          long oldseed = seed.get();          long nextseed = (oldseed * multiplier + addend) & mask;          return (int)(nextseed >>> (48 - bits));      }            public static void main(String[] args) {          Random r = new Random();          r.srandSeed(new Date().getTime());          System.out.println(r.random(32));      }  }

4.汉诺塔程序

import java.util.Scanner; public class TowersOfHanoi {         public static void solveTowers( int disks, int sourcePeg,              int destinationPeg, int tempPeg )           {                             if ( disks == 1 )              {                 System.out.printf( "\n%d --> %d", sourcePeg, destinationPeg );                 return;              }                                            solveTowers( disks - 1, sourcePeg, tempPeg, destinationPeg );                              System.out.printf( "\n%d --> %d", sourcePeg, destinationPeg );                             solveTowers( disks - 1, tempPeg, destinationPeg, sourcePeg );           }            public static void main( String[] args )           {               System.out.print("请输入要移动的盘子数:");                @SuppressWarnings("resource")                Scanner sc=new Scanner(System.in);                @SuppressWarnings("unused")                int  N=sc.nextInt();                solveTowers( N, 1, 2, 3 );                            }         }

 

课后作业2