日期:2014-05-20 浏览次数:20653 次
package com.mater.impl;
public class MinMaterImpl {
public static void main(String[] args){
int n = 80;
int[] p = {30,28, 20};
int s = 1;
MinMaterImpl demo = new MinMaterImpl();
int[][] k = demo.acc(n,p,s);
for(int i = 0; i < k.length;i++){
for(int j = 0; j < k[i].length;j++){
System.out.print(k[i][j]+"\t");
}
System.out.println();
}
}
private int[][] acc(int eMaterial, int[] pro, int s) {
// TODO Auto-generated method stub
int n = pro.length;
int[][] scheme =new int[n+1][15];
int[] m = new int[n];
int k = 0;
for(int i =0; i < n; i++){
scheme[i][0] = pro[i];
m[i] = eMaterial/pro[i];
}
for(int i1 = 0; i1 <= m[0];i1++ ){
for(int i2 = 0;i2 <=m[1];i2++){
for(int i3 = 0;i3 <= m[2];i3++){
int r = eMaterial - i1 *(pro[0]+s) - i2 * (pro[1]+s)- i3*(pro[2]+s);
if(r == -1){
r = 0;
}
if(r >= 0 && r < pro[2]){
k++;
scheme[0][k] = i1;
scheme[1][k] = i2;
scheme[2][k] = i3;
scheme[3][k] = r;
}
}
}
}
return scheme;
}
}
package csdn;
public class MinMaterImpl {
public static void main(String[] args) {
int n = 80;
// int[] p = { 30, 28, 20 };
int[] p = { 30, 28, 20, 16, 11 };
int s = 1;
MinMaterImpl demo = new MinMaterImpl();
int[][] ak = demo.acc(n, p, s);
for (int i = 0; i < ak.length; i++) {
for (int j = 0; j < ak[i].length; j++) {
System.out.print(ak[i][j] + "\t");
}
System.out.println();
}
}
public void fun(int eMaterial, int[] pro, int s, int[] rst, int[][] scheme,
int[] m, int n) {
for (int i = 0; i <= pro[pro.length - n]; ++i) {
rst[pro.length - n] = i;
if (n > 1) {
fun(eMaterial, pro, s, rst, scheme, m, n - 1);
} else {
int r = eMaterial;
for (int j = 0; j < rst.length; ++j) {
r = r - rst[j] * (pro[j] + s);
}
if (r == -1) {
r = 0;
}
if (r >= 0 && r < pro[2]) {
if (k < 14) {
k++;
}
for (int j = 0; j < rst.length; ++j) {
scheme[j][k] = rst[j];
}
scheme[rst.length][k] = r;
}
}
}
}
static int k = 0;
private int[][] acc(int eMaterial, int[] pro, int s) {
// TODO Auto-generated method stub
int n = pro.length;
int[][] scheme = new int[n + 1][25]; // 你的p数组变大的同时,应该增大该数组第一维的值,否则会超出数组范围
int[] m = new int[n];
int[] rst = new int[pro.length];
for (int i = 0; i < n; i++) {
scheme[i][0] = pro[i];
m[i] = eMaterial / pro[i];
}
for (int i1 = 0; i1 <= m[0]; i1++) {
rst[0] = i1;
if (n > 1) {
fun(eMaterial, pro, s, rst, scheme, m, n - 1);
}
}
return scheme;
}
}