hooyantsing's Blog

第10次课程

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

源辰74班

第10次课程

05.06.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
public class Test1 {
    public static void main(String[] args) {
       // 银行数目
       int n = 5;
       // 银行安全线
       double limit = 201;
       // 每家银行的现金余额
       double[] balance = new double[] {  25, 125, 175, 75, 181 };
       // 存借贷关系
       double[][] load = new double[][] { 
        {  0, 100.5, 0, 0, 320.5 },
        { 0, 0, 40, 85, 0  }, 
        { 125, 0, 0, 75, 0 },
        { 125, 0, 0, 0, 0 }, 
        { 0, 0,  125, 0, 0 } };
       // 每个银行的净资产
       double[] assets = new double[n];
       // boolean型默认为false,这里均改为true
       boolean[] isSafe = new boolean[n];
       for (int i = 0; i < isSafe.length;  i++) {
           isSafe[i] = true;
       }
       boolean newUnSafeBank = true;
       while (newUnSafeBank) {
           newUnSafeBank = false;
           // 查找不安全的银行
           for (int i = 0; i < n; i++) {
               // 求出净资产
               assets[i] = balance[i];
               for (int j = 0; j < n; j++) {
                  assets[i] = assets[i] +  load[i][j];
               }
               // 判断银行是否低于安全线以下
               // 仅判断之前已认为安全的银行(红字)
               if (isSafe[i]==true &&  assets[i]<limit) {
                  newUnSafeBank = true;
                  isSafe[i] = false;
                  System.out.println("第" +  i + "家银行不安全");
                  // 将该银行二维数组中i这一列均置为0
                  for (int k = 0; k < n;  k++) {
                      load[k][i] = 0;
                  }
               }
           }
       }
       printArr(load);
    }
    public static void printArr(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();
       }
    }
}

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
60
61
62
63
64
65
66
import java.util.Scanner;
public class Test2 {
    public static void main(String[] args) {
       int n = 3;
       double[][] matrix1 = inputMatrix(n);
       double[][] matrix2 = inputMatrix(n);
       double[][] result =  addMatrix(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];
       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[][]  addMatrix(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++) {
               result[i][j] = a[i][j] +  b[i][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) {
       for(int i=0;i<arr1.length;i++) {
           for(int j=0;j<arr1[i].length;j++)  {
               System.out.print(arr1[i][j]+"   ");
           }
           if(i==arr1.length/2) {
               System.out.print(" "+op+" ");
           }else {
               System.out.print("   ");
           }
           for(int j=0;j<arr2[i].length;j++)  {
               System.out.print(arr2[i][j]+"   ");
           }
           if(i==arr1.length/2) {
               System.out.print(" = ");
           }else {
               System.out.print("   ");
           }
           for(int j=0;j<arr3[i].length;j++)  {
               System.out.print(arr3[i][j]+"   ");
           }
           System.out.println();
       }
    }
}

3.矩阵相乘

DecimaiFormat使用方法

用于限定double小数位输出

1
2
3
4
5
//限定double小数位数,四舍五入。
import java.text.DecimalFormat;
DecimalFormat df = new  DecimalFormat("0.00");
double i = 3.33333333333 ;
System.out.print(df.format(i));
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
import java.text.DecimalFormat;
import java.util.Scanner;
public class Test3 {
    public static void main(String[] args) {
       int n = 3;
       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];
       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++) {
                               //存在bug
               result[i][j] =  a[i][0]*b[0][j]+a[i][1]*b[1][j]+a[i][2]*b[2][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++) {
           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("   ");
           }
           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("   ");
           }
           for(int j=0;j<arr3[i].length;j++)  {
               System.out.print(df.format(arr3[i][j])+"   ");
           }
           System.out.println();
       }
    }
}
CATALOG