Fibonacci sequence
Import Java.util.Scanner;
public class SSSs
{
public static void Main (string[] args)
{
Scanner sc = new Scanner (system.in);
int n = sc.nextint ();
System.out.print (FIB (n));
}
public static int fib (int n)
{
int F0, F1, F2;
F0 = F1 = 1;
F2 = 0;
if (n < 2)
return F1;
for (int i = 2; I <= n; i++)
{
F2 = F0 + F1;
F0 = F1;
F1 = F2;
}
return F2;
}
}
Matrix multiplication
Import Java.util.Scanner;
public class Hell2
{
public static void Main (string[] args)
{
Scanner sc = new Scanner (system.in);
int n = sc.nextint ();
int m[][] = new Int[n + 1][n + 1];
int s[][] = new Int[n + 1][n + 1];
int p[] = new Int[n + 1];
for (int i = 0; I <= N; i++)
{
P[i] = Sc.nextint ();
}
Matrixchain (P, N, M, s);
System.out.println (TraceBack (1, N, s));
}
public static void Matrixchain (int p[], int n, int m[][], int s[][]) {
for (int i = 1; I <= n; i++)//The number of matrices is one, multiply by 0
{
M[i][i] = 0;//m[1][1]
}
for (int r = 2; r <= N; r++)//The number of matrices is 2 until n
{
for (int i = 1; I <= n-r + 1; i++)//matrix starting from the first
{
Int J = i + r-1;;/ /j=2
M[I][J] = m[i + 1][j] + p[i-1] * p[i] * p[j];//m[1][2]=m[2][2]+p[0][1][2]
S[I][J] = I;//s[1][2]=1
for (int k = i; k < J; k++)
{
int t = m[i][k] + m[k + 1][j] + p[i-1] * p[k] * p[j];//t=m[1][1]+m[2][2]+p[0][1][2]
if (T < M[i][j])
{
M[I][J] = t;
S[I][J] = k;
}
}
}
}
}
public static String traceBack (int i, int j, int s[][])
{
if (i = = j)
{
Return "A" + i;
}
Else
{
Return "(" + traceBack (i, s[i][j], s) + TraceBack (S[i][j] + 1, j, s) + ")";
}
}
}
Knapsack problem
Import Java.util.Scanner;
public class Hell3
{
public static void Main (string[] arg0)
{
Scanner sc = new Scanner (system.in);
int n = sc.nextint ();//Number of items
int c = Sc.nextint ();//Backpack capacity
int m[][] = new Int[n + 1][c + 1];//Maximum value
int v[] = new Int[n + 1];//record the value of each item
int w[] = new Int[n + 1];//record the weight of each item
int x[] = new Int[n + 1];//record whether the item is put into the backpack
for (int i = 1; I <= n; i++)//deposit the weight of each item
{
W[i] = Sc.nextint ();
}
for (int i = 1; I <= n; i++)//deposit value of each item
{
V[i] = Sc.nextint ();
}
PA (c, N, V, W, M);
Out (C,n,x,w, M);
for (int i = 1; I <= n; i++)
{
System.out.print (X[i] + "");
}
}
Bottom-up algorithm for knapsack problem
public static void PA (int c, int n, int v[], int w[], int m[][])
{
int jmax = Math.min (W[n]-1, c);
for (int i = 0; I <= jmax; i++)
{
M[n][i] = 0;
}
for (int i = w[n]; I <= c; i++)
{
M[n][i] = V[n];
}
for (int i = n-1; I >= 1; i--)
{
Jmax = Math.min (W[i]-1, c);
for (int j = 0; J <= Jmax; j + +)
{
M[I][J] = m[i + 1][j];
}
for (int j = w[i]; J <= C; j + +)
{
M[I][J] = Math.max (M[i + 1][j], M[i + 1][j-w[i]] + v[i]);
}
}
}
The optimal solution of the algorithm
public static void out (int c,int n,int x[], int w[], int m[][])
{
for (int i = 1; i < n; i++)
{
if (m[i][c] = = M[i + 1][c])
{
X[i] = 0;
}
Else
{
X[i] = 1;
c = C-w[i];
}
}
X[n] = (m[n][c] = = 0? 0:1);
}
}
Two-fork Tree
Import Java.util.Scanner;
public class Hell4
{
public static void Main (string[] args)
{
Scanner sc = new Scanner (system.in);
int n = sc.nextint ();
int p[] = new Int[n + 1];//chance of success
int q[] = new Int[n + 1];//chance of failure
int s[][] = new Int[n + 2][n + 1];//Best node
int w[][] = new Int[n + 2][n + 1];
int m[][] = new Int[n + 2][n + 1];
for (int i = 1; I <= n; i++)
{
P[i] = Sc.nextint ();
}
for (int i = 0; I <= N; i++)
{
Q[i] = Sc.nextint ();
}
Bost (n, p, Q, W, M, s);
T (1, N, s);//optimal solution
}
A dynamic programming algorithm for the bottom-up non-recursion of the optimal binary search tree.
public static void Bost (int n, int p[], int q[], int w[][], int m[][], int s[][])
{
for (int i = 0; I <= N; i++)
{
5 15 10 5 10 20 5 10 5 5 5 10
W[i + 1][i] = Q[i];
M[i + 1][i] = Q[i];
}
for (int r = 0; r < N; r++)
{
for (int i = 1; I <= n-r; i++)
{
Int J = i + r;//j=1
W[I][J] = W[i][j-1] + p[j] + q[j];//w[1][1]=w[1][0]+p[1]+q[1]
M[I][J] = M[i][i-1] + m[i + 1][j];//m[1][1]=m[1][0]+m[2][1]
S[I][J] = I;//s[1][1]=1
for (int k = i + 1; k <= J; k++)
{
int t = m[i][k-1] + m[k + 1][j];//t=m[1][1]+m[3][1]
if (T < M[i][j])
{
M[I][J] = t;
S[I][J] = k;
}
}
M[I][J] + = w[i][j];
}
}
}
public static void T (int i, int j, int s[][])//Structural optimal solution
{
if (J > I)
{
int root = s[i][j];//root node
System.out.println ("s" + root + "is root");
if (S[i][root-1] > 0)
{
System.out.println ("s" + root + "Left child is S" + s[i][root-1]);
}
if (S[root + 1][j] > 0)
{
System.out.println ("s" + root + "Right child is S" + S[root + 1][j]);
}
T (i, root-1, s);
T (root + 1, j, s);
}
}
}
Algorithms used in algorithm analysis Class 2