本文目录一览:
~~求解~~用动态规划算法求两数组各元素间差的最小值,JAVA代码或方法思路
import java.util.Arrays;
public class Test {
public static void getCha(int [] a,int []b){
int min =Integer.MAX_VALUE;
int sss=0;
int kkk = 0;
int c = 0;
int d = 0;
for (int i = 0; i a.length; i++) {
for (int j = 0; j b.length; j++) {
int temp = Math.abs(a[i]-b[j]);
if(tempmin){
min = temp;
sss = a[i];
kkk = b[j];
c=i;
d=j;
}
}
}
System.out.println("最大差距:"+min+"数组A["+c+"]"+sss+"数组B["+d+"]"+kkk);
}
public static void main(String[] args) {
int []a = new int[8];
int []b = new int[12];
for (int i = 0; i a.length; i++) {
a[i] = (int)( Math.random()*100);
}
System.out.println(Arrays.toString(a));;
for (int i = 0; i b.length; i++) {
b[i] = (int) (Math.random()*100);
}
System.out.println(Arrays.toString(b));
getCha(a,b);
}
}
java动态规划 计算数n由k个数相加而成的情况数
public class MyClass {
public static void main(String[] args) {
System.out.println(" result count:" + method(6, 3));
}
public static int method(int n, int k) {
ListListInteger list = new ArrayList();
for (int i = 0; i k; i++) {
if (i == 0) {
for (int j = 1; j n; j++) {
ListInteger li = new ArrayList();
li.add(j);
list.add(li);
}
continue;
}
ListListInteger listNew = new ArrayList();
for (ListInteger integers : list) {
for (int j = integers.get(integers.size() - 1); j n; j++) {
ListInteger li = new ArrayList();
li.addAll(integers);
li.add(j);
listNew.add(li);
if (i + 1 == k) {
int res = 0;
for (Integer integer : li) {
res += integer;
}
if (res != n) {
listNew.remove(li);
}
}
}
}
list.clear();
list.addAll(listNew);
}
for (ListInteger integers : list) {
for (Integer integer : integers) {
System.out.print(integer + "\t");
}
System.out.println();
}
return list.size();
}
}
怎么设计用动态规划算法和java实现最小
import java.util.Arrays;
public class Test {
public static void getCha(int [] a,int []b){
int min =Integer.MAX_VALUE;
int sss=0;
int kkk = 0;
int c = 0;
int d = 0;
for (int i = 0; i a.length; i++) {
for (int j = 0; j b.length; j++) {
int temp = Math.abs(a[i]-b[j]);
if(tempmin){
min = temp;
sss = a[i];
kkk = b[j];
c=i;
d=j;
}
}
}
System.out.println("最大差距:"+min+"数组A["+c+"]"+sss+"数组B["+d+"]"+kkk);
}
public static void main(String[] args) {
int []a = new int[8];
int []b = new int[12];
for (int i = 0; i a.length; i++) {
a[i] = (int)( Math.random()*100);
}
System.out.println(Arrays.toString(a));;
for (int i = 0; i b.length; i++) {
b[i] = (int) (Math.random()*100);
}
System.out.println(Arrays.toString(b));
getCha(a,b);
}
}