一.利用线性同余法生成随机数
特殊之处:方法名一样,但是输出结果不一样!
方法名相同区分的方式(用square方法为例)
·参数类型不同,square(int x),square(double y)
·参数个数,square(int x,int y)square(int x)
·参数类型顺序不同,square(int x,double y)square(double x,int y)
二.在SquareIntTest中,把Square 方法中的static删了会有什么情况
·程序无法运行,并报错
·在main方法中,用类SquareIntTest定义一个新的类名字即SquareIntTest s=new SquareIntTest();这样,在调用方法既可以类名.方法名来调用方法,即s.square(x);
课后作业
一.组合数问题
杨辉三角 :
1 import java.io.*;
2 public class CombinatorialNumber {
3 public static void main(String[] args) throws IOException{
4 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
5 String num1 = reader.readLine(); String num2 = reader.readLine();
6 int n = Integer.parseInt(num1);int k = Integer.parseInt(num2);
7 if(n>=2&&k>1&&(n>=k))
8 {
9 int sum1,sum2,sum3;
10 sum1 = nStratum(n,k);
11 System.out.println("The combinatorial number of (n,k)is " + sum1);
12 sum2 = triangleYH(n,k);
13 System.out.println("The combinatorial number of (n+1,k)is " + sum2);
14 sum3 = digui(n,k);
15 System.out.println("The combinatorial number of (n,k)is " + sum3);
16 }
17 else if(n>=2&&k==1)
18 {
19 System.out.println("The combinatorial number of (n,k)is " + n);
20 }
21 else if(n==1&&k==1)
22 {
23 System.out.println("The combinatorial number of (n,k)is 1.");
24 }
25 else
26 {
27 System.out.println("Error!please input again!");
28 }
29 }
30
31 public static int nStratum(int n,int k)//阶乘
32 {
33 int n_stratum=1,k_stratum=1,n_k_stratum=1,sum=1;
34 int i;
35 for(i=1;i<=n;i++) { n_stratum=n_stratum*i; }
36 for(i=1;i<=k;i++) { k_stratum=k_stratum*i; }
37 for(i=1;i<=(n-k);i++) { n_k_stratum=n_k_stratum*i; }
38 sum = n_stratum/(k_stratum*n_k_stratum);
39 return sum;
40 }
41 public static int digui(int n,int k)//递归
42 {
43 if(k==1) return n;
44 else
45 {
46 int sum = (digui(n,k-1))*(n-1)/k;
47 return sum;
48 }
49 }
50 public static int triangleYH(int n,int k)//杨辉三角
51 {
52 int sum,sum1,sum2;
53 sum1 = digui(n,k);
54 sum2 = digui(n,k-1);
sum = sum1+sum2;
return sum;
}
}
二.课后作业2递归编程解决汉诺塔问题。用Java实现
import java.io.*;
public class TowersOfHanoi{
public static void main( String[] args ) throws IOException{
System.out.println("输入盘子的个数:");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String readnum = reader.readLine();
int n = Integer.parseInt(readnum);
solveTowers(n,'A','B','C');
}
public static void solveTowers(int n,char a,char b,char c){
if (n == 1)
System.out.println("盘 " + n + " 由 " + a + " 移至 " + c);
else {
solveTowers(n - 1, a, c, b);
System.out.println("盘 " + n + " 由 " + a + " 移至 " + c);
solveTowers(n - 1, b, a, c);
}
}
}
三.课后作业3使用递归方式判断某个字串是否是回文( palindrome )
import java.io.*;
public class Huiwen {
public static void main(String[] args)throws IOException {
System.out.println("请输入一个字符串:");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str=reader.readLine();
if(huijudge(str)==1)
System.out.println(str+"是回文!");
else
System.out.println(str+"不是回文!");
}
public static int huijudge(String str){
int judge=1;
int length=str.length();
//charAT()是把字符串拆分获取其中的某个字符,返回指定位置的字符。
char f=str.charAt(0);
char l=str.charAt(length-1);
if(length==0||length==1)
return judge=1;
if(f!=l)
return judge=0;
//substring() 方法用于提取字符串中介于两个指定下标之间的字符。
return huijudge(str.substring(1,length-1));
}
}