Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public double findSuitableDensity(int[] v, int[] w) {
int s=v.length;
double[] d= new double[s];
for(int i=0; i<s; i++){
d[i]=(double)w[i]/(double)v[i];
}
double minDS=0.0;
boolean first=true;
for(int ii=0; ii<s; ii++){
double[] newW= new double[s];
for(int i=0; i<s; i++){
newW[i]=(double)d[ii] * (double)v[i];
}
double diffSum=0.0;
for(int i=0; i<s; i++){
diffSum+=Math.abs((double)newW[i]-(double)w[i]);
}
if(first){
minDS=diffSum;
first=false;
}else{
if(diffSum<minDS)
minDS=diffSum;
}
}
return minDS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment