动手动脑1:
package test;
public class sgg
{
private static final int N = 200;
private static final int LEFT = 40;
private static final int RIGHT = 10000;
private static long x0 = 1L;
private long a = 1103515245L;
private long c = 12345L;
private long m = 2147483648L;
private long rand ( long r )
{
r = ( r * a + c ) % m;//Xn+1=(aXn + c)mod m
return r;
}
private long little ( int a, int b, long rand )
{
return a + rand % ( b - a + 1 );
}
private void recursion ( int count, long rand )
{
if (count >= N)
{
return;
}
rand = rand (rand);
long r = little (LEFT, RIGHT, rand);
System.out.print (r + "
");
recursion (++count, rand);
}
public static void main ( String[] args )
{
sgg recur = new sgg ();
recur.recursion (0, x0);
}
}
动手动脑2:以下程序有什么特殊之处?
在主函数下边还有两个static结构构成的成员方法。
课后作业1:计算组合数
1)程序:package test;
import java.util.*;
public class sgg {
public static void main(String[] args) {// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.print("please input:");
int n = in.nextInt();
System.out.print("please input:");
int k = in.nextInt();
int C = sgg(n)/(sgg(k)*sgg(n - k));
System.out.println("reasult is:"+C);
}
public static int sgg(int n)//递归法计算阶乘
{
int s = 0;
if(n < 0)
System.out.println("错误!");
else if(n == 1||n == 0)
s = 1;
else
s = n * sgg(n -1);
return s;
}
}
)程序:
package test;
import java.util.Scanner;
public class sgg {
public static void main(String[] args) {// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.print("请输n:");
int n = in.nextInt();
System.out.print("请输入k:");
int k = in.nextInt();
int f = sgg(n,k);
System.out.println(" 公式计算结果为: "+f);
}
public static int sgg(int n,int k)
{
int f = 0;
if(n == 1||k == 0||n == k)
f = 1;
else
f = sgg(n - 1,k - 1) + sgg(n - 1,k);
return f;
}
}
结果输出:
3)程序:package test;
import java.util.Scanner;
public class sgg {
public static void main(String[] args) {// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.print("请输入n:");
int n = in.nextInt();
System.out.print("请输入k:");
int k = in.nextInt();
System.out.println("组合数结果为:"+sgg(n,k));
in.close();
}
public static int sgg(int m,int n)
{
if(m<0||n<0||m<n)
return 0;
if(m==n)
return 1;
if(n==1)
return m;
return sgg(m-1,n)+sgg(m-1,n-1);
}
}
输出结果:
课后作业2:汉诺塔用JAVA实现
程序:
// TowersOfHanoi.java
// Towers of Hanoi solution with a recursive method.
public class TowersOfHanoi
{
// recursively move disks between towers
public static void solveTowers( int disks, int sourcePeg,
int destinationPeg, int tempPeg )
{
// base case -- only one disk to move
if ( disks == 1 )
{
System.out.printf( "
%d --> %d", sourcePeg, destinationPeg );
return;
} // end if
// recursion step -- move (disk - 1) disks from sourcePeg
// to tempPeg using destinationPeg
solveTowers( disks - 1, sourcePeg, tempPeg, destinationPeg );
// move last disk from sourcePeg to destinationPeg
System.out.printf( "
%d --> %d", sourcePeg, destinationPeg );
// move ( disks - 1 ) disks from tempPeg to destinationPeg
solveTowers( disks - 1, tempPeg, destinationPeg, sourcePeg );
} // end method solveTowers
public static void main( String[] args )
{
int startPeg = 1; // value 1 used to indicate startPeg in output
int endPeg = 3; // value 3 used to indicate endPeg in output
int tempPeg = 2; // value 2 used to indicate tempPeg in output
int totalDisks = 3; // number of disks
// initial nonrecursive call: move all disks.
solveTowers( totalDisks, startPeg, endPeg, tempPeg );
} // end main
} // end class TowersOfHanoi
输出结果:
课后作业3:判断字符串是否为回文数
程序:
package test;
import java.util.*;
public class sgg {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="";
System.out.println("请输入一个字符串:");
Scanner in=new Scanner(System.in);
str=in.nextLine();
StringBuffer sb=new StringBuffer(str);
sb.reverse();
int n=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)==sb.charAt(i))
n++;
}
if(n==str.length())
System.out.println(str+"是回文!");
else
System.out.println(str+"不是回文!");
}
}
结果输出: