After-school Assignments 3
1. Design Ideas
The input string loops through recursive functions to determine whether the characters in each position of the string are equal
2. Program Flowchart
3. Source code
import Java.util.Scanner;
Public class Huiwen {
Public Static boolean ispalindrome (String s,int i,int j) {
if (i = = j)
return true;
Else {
return (S.charat (i) = = S.charat (j)) && ispalindrome(s,i+1,j-1);
}
}
Public Static void Main (string[] args) {
TODO auto-generated Method stub
Scanner scanner=New Scanner (System. in);
String s = scanner.nextline ();
int i = 0;
int j = s.length ()-1;
if (Huiwen. Ispalindrome (S,I,J))
System. out. println (s+ "is the back text string");
Else
System. out. println (s+ "not a text string");
}
}
4. Results
After-school Assignments 2
1. Design Ideas
Idea: To move the n disk from pillar 1 to pillar 3, in accordance with the large disk in the bottom order of stacking up, each step can only be one of the top of a column to move to the top of another pillar, to solve this problem, because the disk from the Pillar 1 to the Pillar 3, the largest plate must first move to the Pillar 3. To achieve this, the pillar 3 must be empty, and the pillar 1 can only have the largest disk, so the rest of the n-1 disk only on the Pillar 2. Assuming that the function move can move the n-1 on the pillar 1 to the Pillar 2, and the Pillar 1 on the bottom (maximum) of the disk to the column 3, then only then use the move, the column 2 on the n-1 disk to the Pillar 3 can complete the task.
2. Program Flowchart
3. Source code
Import java.util.*;
public class Hannuota {
public static void tower (int n,char One,char Two,char three) {
if (n==1)
{
Move (one,three,1);
}
Else
{
Tower (N-1,one,three,two);
Move (One,three, N);
Tower (N-1,two,one,three);
}
}
Output
public static void Move (char x,char y, int n) {
System.out.println (x+ "The" +n+ "disc moved to" +y ");
}
public static void Main (string[] args) {
int n;
Scanner in = new Scanner (system.in);
System.out.println ("Please enter the number of plates:");
n = in.nextint ();
Hannuota.tower (n, ' A ', ' B ', ' C ');
}
}
4. Results
After-school assignments 1
1. Design Ideas
Use the combination number formula to calculate with n!
Using recursive methods to calculate with Yang Hui triangles
2. Program Flowchart
3. Source code
Import java.util.*;
Import Javax.swing.JOptionPane;
public class Jiecheng {
public static long fact (int k,int N) {
if (k==0| | n==0)
return 1;
if (n<k)
return 0;
if (n==k)
return 1;
return fact (k-1,n-1) +fact (k,n-1);
}
public static long Jiecheng (int i) {
if (i==1| | i==0)
return 1;
Else
Return I*jiecheng (i-1);
}
public static void Main (string[] args) {
String n1,n2;
int i,j;
Scanner input=new Scanner (system.in);
N1=joptionpane.showinputdialog ("Enter the value of K:");
N2=joptionpane.showinputdialog ("Enter the value of N:");
i= Integer.parseint (N1);
j= Integer.parseint (n2);
Joptionpane.showmessagedialog (
NULL, "Using factorial calculation:" +jiecheng (j)/(Jiecheng (i) *jiecheng (j-i)));
Joptionpane.showmessagedialog (
NULL, "Using Yang Hui triangle calculation:" +fact (I,J));
}
}
4. Results
Java Course Assignment 02