java 课后作业

Posted 宫aaaa1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 课后作业相关的知识,希望对你有一定的参考价值。

动手动脑1:

Modulus=231-1=int.MaxValue Multiplier=75=16807 C=0 当显示过231-2个数之后,才可能重复。

编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数。

 

随机数发生器

public class SuiJi
{
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 )
{
// a,c,m为常数
r = ( r * a + c ) % m;//Xn+1=(aXn + c)mod m
return r;
}

/**
* 表示a~b之间的一个随机数

* @param a
* @param b
* @param rand
* @return
*/
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 )
{
suiji recur = new suiji ();
recur.recursion (0, x0);
}
}

 

动手动脑2:

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

 上述示例代码展示了Java的“方法重载(overload)”特性。满足以下条件的两个或多个方法构成“重载”关系:1.方法名相同(square)2.参数类型不同(int,double),参数个数不同或参数类型的顺序不同。

 

课后作业1:

(1)使用组合数公式利用n!来计算

package text;
import java.util.*;
public class jiecheng {

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 C = Jiecheng(n)/(Jiecheng(k)*Jiecheng(n - k));
System.out.println("组合数的结果为:"+C);

}
public static int Jiecheng(int n)//递归法计算阶乘
{
int s = 0;
if(n < 0)
System.out.println("Error!");
else if(n == 1||n == 0)
s = 1;
else
s = n * Jiecheng(n -1);
return s;
}
}

(2)使用递推的方法用杨辉三角形计算技术分享

package text;
import java.util.Scanner;
public class yanghuisanjiao {

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 = CombinationNumber(n,k);
System.out.println(" 公式计算结果为: "+f);
}
public static int CombinationNumber(int n,int k)
{
int f = 0;
if(n == 1||k == 0||n == k)
f = 1;
else
f = CombinationNumber(n - 1,k - 1) + CombinationNumber(n - 1,k);
return f;

}

}

 

(3)使用递归的方法用组合数递推公式计算技术分享

package text;
import java.util.Scanner;
public class ditui {

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("组合数结果为:"+jieguo(n,k));
in.close();

}
public static int jieguo(int m,int n)
{
if(m<0||n<0||m<n)
return 0;
if(m==n)
return 1;
if(n==1)
return m;
return jieguo(m-1,n)+jieguo(m-1,n-1);
}

}

课后作业2:

递归编程解决汉诺塔问题

package text;
import java.util.*;
public class hannuota {


// 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( "\\n%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( "\\n%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 );
}

}

 

课后作业3:

使用递归方式判断某个字串是否是回文

package text;
import java.util.*;
public class huiwen {

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+"不是回文!");
}

}

 
 
 

以上是关于java 课后作业的主要内容,如果未能解决你的问题,请参考以下文章

09《JAVA从零到壹》第九讲:异常处理(文末有课后作业)

java 课后作业

java课后作业-4

java课后作业1

08《JAVA从零到壹》第八讲:系统常用类(文末有课后作业)

# 20165210 课后代码作业练习