Java after-school Exercise 3

Source: Internet
Author: User
Tags stub stringbuffer

Hands on brain 1: Handwritten random code

Program: 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;
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 + "\ n");
Recursion (++count, Rand);
}
public static void Main (string[] args)
{
SGG recur = new SGG ();
Recur.recursion (0, x0);
}
}

Brain 2: What's special about the following procedures?

Below the main function, there are two member methods composed of a static structure.

After-school assignment 1: Calculating the number of combinations

1) Procedure: 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 enter N:");
int n = in.nextint ();
System.out.print ("Please enter K:");
int k = In.nextint ();

int C = SGG (n)/(SGG (k) *sgg (n-k));
SYSTEM.OUT.PRINTLN ("The result of the combination number is:" +c);
}

public static int Sgg (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 * SGG (n-1);
return s;
}
}

Output results

2) Procedure:

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 ("Please Lose N:");
int n = in.nextint ();
System.out.print ("Please enter K:");
int k = In.nextint ();

int f = SGG (n,k);
SYSTEM.OUT.PRINTLN ("The formula calculates the result:" +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;
}
}

Result output:

3) Procedure: 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 ("Please enter N:");
int n = in.nextint ();
System.out.print ("Please enter K:");
int k = In.nextint ();
SYSTEM.OUT.PRINTLN ("Combined number result is:" +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);
}
}

Output Result:

After-school assignment 2: Hanoi with Java implementation

Program:

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 ("\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);
}//End Main
}//End Class Towersofhanoi

Output Result:

Homework 3: Judging whether a string is a palindrome number

Program:

Package test;
Import java.util.*;
public class Sgg {
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!");
}
}

Result output:

Java after-school Exercise 3

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.