算法复习___二分_矩阵连乘
IoUtil接口:
import java.util.List; public interface IoUtil { public int getInt(); public List<Integer> getIntList(int n); public int[] getIntArray(int n); public double getDouble(); public List<Double> getDoubleList(int n); public double[] getDoubleArray(int n); }
IoUtilImpl接口实现类:
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class IoUtilImpl implements IoUtil { public static Scanner cin; static{ cin = new Scanner(System.in); } @Override public double getDouble() { return cin.nextDouble(); } @Override public List<Double> getDoubleList(int n) { ArrayList<Double> list = new ArrayList<Double>(); for(int i = 0; i < n; i++){ list.add(cin.nextDouble()); } return list; } @Override public int getInt() { return cin.nextInt(); } @Override public List<Integer> getIntList(int n) { ArrayList<Integer> list = new ArrayList<Integer>(); for(int i = 0; i < n; i++){ list.add(cin.nextInt()); } return list; } @Override public double[] getDoubleArray(int n) { double[] m = new double[n]; for(int i = 0; i < n; i++){ m[i] = cin.nextDouble(); } return m; } @Override public int[] getIntArray(int n) { int[] m = new int[n]; for(int i = 0; i < n; i++){ m[i] = cin.nextInt(); } return m; } }
二分主类:
import java.util.Arrays; public class 二分 { public static int maxi, minj; public static boolean erFen(int[] m, int l, int r, int x){ int mid; while(l <= r){ mid = (l + r) / 2 ; if(x == m[mid]){ maxi = minj = mid; return true; } if(x <= m[mid]) r = mid - 1; else l = mid + 1; } maxi = l - 1; minj = l; return false; } public static void main(String[] args) { int n; int a[]; int x; IoUtilImpl util; util= new IoUtilImpl(); System.out.println("请输入数组长度n:"); n = util.getInt(); System.out.println("请输入 " + n + " 个数组元素"); a = util.getIntArray(n); Arrays.sort(a); while(true){ System.out.println("请输入要查找的元素x(x=0 时退出)"); x = util.getInt(); if(x == 0){ break; } if(erFen(a, 0, n - 1, x)){ System.out.println("可以查到元素 " + x); System.out.println("该元素在第 " + maxi + " 个位置"); }else{ System.out.println("不能查到元素" + x); System.out.println("元素 " + x + " 在位置 " + maxi + " 和位置 " + minj + " 之间"); } } } }
矩阵连乘:不知道对不对,好像错了,先放着:
import java.util.Scanner; class Matrix{ int l; int r; static Scanner cin; static{ cin = new Scanner(System.in); } public Matrix() { } public Matrix(int l, int r){ this.l = l; this.r = r; } public void input(){ l = cin.nextInt(); r = cin.nextInt(); } } public class 矩阵连乘 { public static Matrix[] matrix; public static int[][] m; public static int n; public static IoUtilImpl util; static{ util = new IoUtilImpl(); } static void init(){ for(int i = 0; i < n; i++){ m[i][i] = 0; int l = matrix[i].l, mid, r; int sum = 0; for(int j = i + 1; j < n; j++){ mid = matrix[j].l; r = matrix[j].r; sum += l * mid * r; m[i][j] = m[j][i] = sum; } } } public static void main(String[] args) { System.out.println("请输入矩阵个数n:"); n = util.getInt(); matrix = new Matrix[n]; m = new int[n][n]; System.out.println("请输入这n个矩阵:"); for(int i = 0; i < n; i++){ Matrix x = new Matrix(); x.input(); matrix[i] = x; } init(); for(int i = 0; i < n; i++){ for(int j = i + 1; j < n; j++){ for(int k = i; k < j; k++){ m[i][j] = Math.min(m[i][j], m[i][k] + m[k + 1][j] + matrix[i].l*matrix[k].r*matrix[j].l); } } } System.out.println("最小连乘次数是: " + m[0][n - 1]); } }