算法代码

来源:互联网 发布:罗盘软件 编辑:程序博客网 时间:2024/05/15 15:20

动态规划题目:Say you have an array for which the i-th element is the price of a given stock on day i.Design an algorithm and implement it to nd the maximum pro t. You may complete     at most two transactions.Note: You may not engage in multiple transactions at the same time (ie,you must sell the stock before you buy again).

代码:

import java.io.ObjectInputStream.GetField;
public class Max_Rock_Profit {
static int Rock_Price[] = { 2, 5, 8, 15, 4, 28, 20, 33 };
static int N = Rock_Price.length;
public static int Max_Profit(int a[]) {
int max = Integer.MIN_VALUE;
int Left_min = Integer.MAX_VALUE;
int[] Left_Max = new int[N];
int[] Right_Max = new int[N];


int diff;
for (int i = 0; i < N; i++) {
if (a[i] < Left_min)
Left_min = a[i];
diff = a[i] - Left_min;
if (diff > max)
max = diff;
Left_Max[i] = max;
}
max = Integer.MIN_VALUE;
int Max_Right = Integer.MIN_VALUE;
for (int j = N - 1; j >= 0; j--) {
if (a[j] > Max_Right)
Max_Right = a[j];
diff = Max_Right - a[j];
if (diff > max)
max = diff;
Right_Max[j] = max;
}
max = Integer.MIN_VALUE;
for (int k = 0; k < N - 1; k++) {
int m = Left_Max[k] + Right_Max[k + 1];
if (m > max)
max = m;
}
return max;
}
public static void main(String[] args) {
          System.out.println(Max_Profit(Rock_Price));
}
}

Divide and Conquer(分而治之)
 The attached le Q5.txt contains 100,000 integers between 1 and 100,000 (each row has a single integer), the order of these integers is random and no integer is repeated.
 1. Write a program to implement the Sort-and-Count algorithms in your favorite language, nd the number of inversions in the given le.

求逆序数代码:

package hello;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.imageio.plugins.bmp.BMPImageWriteParam;
import javax.xml.crypto.Data;


public class Hello {
static int[] a = new int[100000];
static int N = 100000;
static long count = 0;


public static void merge_Count(int a[], int m, int l, int r) {


int L[] = new int[l - m + 1];
int R[] = new int[r - l];
int p = 0;
int q = 0;
for (int i = m; i <= l; i++)
L[p++] = a[i];
for (int i = l + 1; i <= r; i++) {
R[q++] = a[i];
}
int i = 0, j = 0;
for (int k = m; k <= r; k++) {
if (i <= l - m & j <= r - l - 1) {
if (L[i] < R[j]) {
a[k] = L[i];
i++;
} else {
a[k] = R[j];
j++;
count = count + l - m -i+ 1;
}
} else if (i > l - m) {
a[k] = R[j++];
} else {
a[k] = L[i++];
}
}
}


public static void inversion_count(int a[], int l, int r) {


if (l < r) {
int m = (l + r) / 2;
inversion_count(a, l, m);
inversion_count(a, m + 1, r);
merge_Count(a, l, m, r);
} else {
return;
}
}


public static void main(String[] args) {


try {
FileReader fr = new FileReader("Q2.txt");
BufferedReader br = new BufferedReader(fr);


int i = 0;
String line;
while ((line = br.readLine()) != null) {
a[i++] = Integer.parseInt(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
inversion_count(a, 0, N - 1);
System.out.println(count);
}
}

Implement the Karatsuba algorithm for Multiplication problem in your favourite language, and compare the performance with quadratic grade-school method.

求大数积代码:

import java.util.Scanner;
public class Karatsuba {
public static int Num_ength(long m){
int length=0;
while(m!=0){
length++;
m=m/10;
}
return length;
}
public  static class split_num{
int hight=0,low=0;
}
public static void  split(long num,long m,split_num a ){
a.hight=(int) (num/Math.pow(10, m));
a.low=(int) (num-a.hight*Math.pow(10, m));
}
  public static long Karatsuba(long num1,long num2) {
split_num aNum1=new split_num();
split_num aNum2=new split_num();
 int m=0;
 if (num1<10 | num2<10)
 return num1*num2;
  if(Num_ength(num1)>Num_ength(num2))
    m=Num_ength(num1)/2;
    else
    m=Num_ength(num2)/2;
 split(num1, m, aNum1);
 split(num2,m,aNum2);
 long z0=Karatsuba(aNum1.low, aNum2.low);
 long z1=Karatsuba((aNum1.low+aNum1.hight),(aNum2.low+aNum2.hight));
 long z2=Karatsuba(aNum1.hight, aNum2.hight);
 return (long) (z2*Math.pow(10, 2*m)+(z1-z2-z0)*Math.pow(10, m)+z0);
}
public static void main(String[] args) {
 Scanner scanner=new Scanner(System.in);
 System.out.print("please enter two big number:");
 long  num1=scanner.nextInt();
 long num2=scanner.nextInt();
 long time1=System.currentTimeMillis();
 System.out.println(Karatsuba(num1, num2));
 long time2=System.currentTimeMillis();
 System.out.println(" Time is:"+(time2-time1));
 long time3=System.currentTimeMillis();
 long num=num1*num2;
 System.out.println(num);
  }
    }


0 0
原创粉丝点击