hooyantsing's Blog

第11次课程

字数统计: 1.1k阅读时长: 6 min
2019/05/08

源辰74班

第11次课程

05.08.2019

内容

1.矩阵相乘(标准版)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.text.DecimalFormat;
import java.util.Scanner;

public class juzhenchengfa {

    public static void main(String[] args) {
        int n = 2;
        double[][] matrix1 = inputMatrix(n);
        double[][] matrix2 = inputMatrix(n);
        double[][] result = multiplyMatrix(matrix1,matrix2);
        //print(result);
        printFormula(matrix1,matrix2,result,'*');

    }
    
    public static double[][] inputMatrix(int n){
        Scanner sc = new Scanner(System.in);
        double[][] arr = new double[n][n];
        System.out.println("请输入矩阵值:");
        for(int i=0;i<arr.length;i++) {
            for(int j=0;j<arr[i].length;j++) {
                arr[i][j] = sc.nextDouble();
            }
        }
        return arr;
    }
    
    public static double[][] multiplyMatrix(double[][]a,double[][]b){
        double[][] result = new double[a.length][a[0].length];

        //矩阵乘法核心算法
        for(int i=0;i<a.length;i++) {
            for(int j=0;j<a[i].length;j++) {
                for(int k=0;k<a[0].length;k++) {
                    result[i][j] = result[i][j] + a[i][k] + b[k][j];
                }
            }
        }
        
        return result;
    }
    
    public static void print(double[][] arr) {
        for(int i=0;i<arr.length;i++) {
            for(int j=0;j<arr[i].length;j++) {
                System.out.print(arr[i][j]+"\t");
            }
            System.out.println();
        }
    }
    
    public static void printFormula(double[][] arr1,double[][] arr2,double[][] arr3,char op) {
        //限定double小数位数,四舍五入。
        DecimalFormat df = new DecimalFormat("0.00");
        //外层循环控制打印行
        for(int i=0;i<arr1.length;i++) {
            //打印arr1
            for(int j=0;j<arr1[i].length;j++) {
                System.out.print(df.format(arr1[i][j])+"  ");
            }
            if(i==arr1.length/2) {
                System.out.print(" "+op+" ");
            }else {
                System.out.print("   ");
            }
            //打印arr2
            for(int j=0;j<arr2[i].length;j++) {
                System.out.print(df.format(arr2[i][j])+"  ");
            }
            if(i==arr1.length/2) {
                System.out.print(" = ");
            }else {
                System.out.print("   ");
            }
            //打印result
            for(int j=0;j<arr3[i].length;j++) {
                System.out.print(df.format(arr3[i][j])+"  ");
            }
            System.out.println();
        }
    }
}

2.矩阵转置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Scanner;
public class juzhenzhuanzhi {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int rows,cols;
       System.out.println("请输入行数:");
       rows = sc.nextInt();
       System.out.println("请输入列数:");
       cols = sc.nextInt();
       int[][] arr = new int[rows][cols];
       int[][] result = new  int[cols][rows];
       arr = inputMatrix(arr);
       System.out.println("矩阵转置前:");
       print(arr);
       result = transpose(arr);
       System.out.println("矩阵转置后:");
       print(result);
    }
    
    private static int[][] transpose(int[][]  arr) {

       //转置核心算法
       //rows -> cols    cols->rows
       int[][] result = new  int[arr[0].length][arr.length];
       for(int i=0;i<arr.length;i++) {
           for(int j=0;j<arr[i].length;j++)  {
               result[j][i] = arr[i][j];
           }
       }

       return result;
    }
    private static int[][]  inputMatrix(int[][] arr) {
       Scanner sc = new Scanner(System.in);
       System.out.println("请输入矩阵中的数据:");
       for(int i=0;i<arr.length;i++) {
           for(int j=0;j<arr[i].length;j++)  {
               arr[i][j] = sc.nextInt();
           }
       }
       return arr;
    }
    
    public static void print(int[][] arr) {
       for(int i=0;i<arr.length;i++) {
           for(int j=0;j<arr[i].length;j++)  {
               System.out.print(arr[i][j]+"   ");
           }
           System.out.println();
       }
    }
}

3.[大数据]人物相似画像

两点之间最短距离

1
2
3
4
5
6
7
int a,b,c,p1,p2;
//相当于:(p1-p2)^2
a = Math.pow(p1-p2, 2);
//相当于:二次根号
b = Math.sqrt(a);
//合并:根号(p1-p2)^2
c = Math.sqrt(Math.pow(p1-p2, 2));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.Scanner;
public class sanweishuzuzuixiaojuli {
    public static void main(String[] args) {
       /*double[][] points = new  double[][]{
           {-1,0,3},
           {-1,-1,-1},
           {4,1,1},
           {2,0.5,9},
           {3.5,2,-1},
           {3,1.5,3},
           {-1.5,4,2},
           {5.5,4,-0.5}
       };*/
       double[][] points = inputData();
       int p1=0,p2=0;
       //设第一个数组和第二个数组之间为最小值,再与后面比较
       double shortDistance =  distance(points[0],points[1]);
       for(int i=0;i<points.length;i++) {
           for(int  j=i+1;j<points.length;j++) {
               double d =  distance(points[i],points[j]);
               System.out.println("第"+i+"点到第"+j+"点的距离为:"+d);
               if(d<shortDistance) {
                  p1 = i;
                  p2 = j;
                  shortDistance=d;
               }
           }
       }
       System.out.println("最短距离是第"+p1+"点到第"+p2+"点的距离为:"+shortDistance);
       
    }
    
    private static double[][] inputData() {
       Scanner sc = new Scanner(System.in);
       String[] films = {"盗梦空间","哈利波特","流浪地球"};
       System.out.println("请输入采样人数:");
       int n = sc.nextInt();
       
       double[][] rates = new  double[n][films.length];
       for(int i=0;i<n;i++) {
           System.out.println("请输入第"+(i+1)+"位人的评分:");
           for(int j=0;j<films.length;j++) {
               System.out.println("请输入您对:《"+films[j]+"》的评分(1,2,3,4,5)");
               rates[i][j] = sc.nextInt();
           }
       }
       
       return rates;
    }
    public static double distance(double[]  point1,double[] point2) {
       //规整化
       double add = 0;
       for(int i=0;i<point1.length;i++) {
           add +=  Math.pow(point1[i]-point2[i], 2);
       }
       return Math.sqrt(add);
       
    }
}
CATALOG