Hands on Brain 1:
Modulus=231-1=int. MaxValue multiplier=75=16807 c=0 It is possible to repeat after displaying 231-2 numbers.
Write a method that uses the above algorithm to generate a random integer of a specified number (for example, 1000).
/**
* Random number generator
*/
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;
Generate random numbers
Private Long rand (long R)
{
A,c,m is constant
R = (R * A + C)% m;//xn+1= (aXn + c) MoD m
return R;
}
/**
* Represents a random number between 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);
}
}
Hands on Brain 2:
Take a look at the following code, do you find anything special?
The example code above shows the Java method overloading (overload) attribute. Two or more methods that meet the following conditions form an "overloaded" relationship: 1. The method name is the same (square) 2. The parameter types are different (int,double), the number of arguments is different, or the order of the parameter types is different.
After-school assignment 1:
(1) Using the combination number formula to calculate with 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 ("Please enter N:");
int n = in.nextint ();
System.out.print ("Please enter K:");
int k = In.nextint ();
int C = Jiecheng (n)/(Jiecheng (k) *jiecheng (n-k));
SYSTEM.OUT.PRINTLN ("The result of the combination number is:" +c);
}
public static int Jiecheng (int n)//recursive calculation factorial
{
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) Using recursive method to calculate with Yang Hui triangle
Package text;
Import Java.util.Scanner;
public class YHSJ {
public static void Main (string[] args) {
TODO auto-generated Method Stub
Scanner in = new Scanner (system.in);
System.out.print ("Please Lose N:");
int n = in.nextint ();
System.out.print ("Please enter K:");
int k = In.nextint ();
int f = combinationnumber (n,k);
SYSTEM.OUT.PRINTLN ("The formula calculates the result:" +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) Using recursive method to calculate the recursive formula of combination number
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 ("Please enter N:");
int n = in.nextint ();
System.out.print ("Please enter K:");
int k = In.nextint ();
SYSTEM.OUT.PRINTLN ("Combined number result is:" +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);
}
}
After-school Assignment 2:
Recursive programming solves the problems of the Nottingham Tower
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-to-%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-to-%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);
}
}
After-school Assignment 3:
Use recursive method to determine whether a string is a palindrome
Package text;
Import java.util.*;
public class Huiwen {
public static void Main (string[] args) {
TODO auto-generated Method Stub
String str= "";
System.out.println ("Please enter a string:");
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+ "is a palindrome!");
Else
System.out.println (str+ "Not a palindrome!");
}
}
Java Classroom Exercise 3