1、 java类中不用static还能如何调用函数?
如果此函数是静态的,会在程序初始化过程中直接进行内存加载,此方法内的所有方法内容必须是静态的,否则会报错静态方法引用动态变量。方法调用:“类名.方法”
如果此函数是动态的,那么只有在调用的时候才会被主动加载一次,之后释放内存。方法调用:必须先实例化出来一个类,之后再通过实例化类名+方法的形式调用。
2、观察以下代码的不同之处?
表示方法为静态,在其他类中可以用类名去调用这个方法。
2、求组合数源程序:
(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; } }
3、回文数程序:
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)); } } }
4、线性同余法生成随机数
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)); } }
5、汉诺塔程序
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( " %d --> %d", sourcePeg, destinationPeg ); return; } solveTowers( disks - 1, sourcePeg, tempPeg, destinationPeg ); System.out.printf( " %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 ); } }