package com
.softeem
.exam2
;
import java
.util
.ArrayList
;
import java
.util
.List
;
public class Test8 {
public static void main(String
[] args
) {
int[][] nums
= new int[5][6];
for (int i
= 0; i
< nums
.length
; i
++) {
for (int j
= 0; j
< nums
[i
].length
; j
++) {
nums
[i
][j
] = (int)(Math
.random()*9+1);
}
}
for (int i
= 0; i
< nums
.length
; i
++) {
for (int j
= 0; j
< nums
[i
].length
; j
++) {
System
.out
.print(nums
[i
][j
]+"\t");
}
System
.out
.println();
}
int[] nos
= new int[3];
List
<Integer> lists
= new ArrayList<Integer>();
lists
.add(nums
[0][0]);
int m
= 0, n
= 0;
while(m
<= 4 && n
<= 5){
if(m
< 4 && n
< 5){
nos
[0] = nums
[m
][n
+1];
nos
[1] = nums
[m
+1][n
];
nos
[2] = nums
[m
+1][n
+1];
int min
= nos
[0];
int index
= 0;
for (int k
= 0; k
< nos
.length
; k
++) {
if(min
>= nos
[k
]){
min
= nos
[k
];
index
= k
;
}
}
if(index
== 0){
lists
.add(nos
[0]);
n
+= 1;
}else if(index
== 1){
lists
.add(nos
[1]);
m
+= 1;
}else{
lists
.add(nos
[2]);
m
+= 1;
n
+= 1;
}
}else if(m
< 4 && n
== 5){
lists
.add(nums
[m
+1][n
]);
m
+= 1;
}else if (m
== 4 && n
< 5){
lists
.add(nums
[m
][n
+1]);
n
+= 1;
}else{
break;
}
}
System
.out
.print("\n最小和是:");
for (int j
= 0; j
< lists
.size(); j
++) {
if(j
== lists
.size()-1){
System
.out
.print(lists
.get(j
));
break;
}
System
.out
.print(lists
.get(j
)+"+");
}
}
}
转载请注明原文地址:https://tech.qufami.com/read-13828.html