我正处于这种奇怪的情况,在这种情况下,除了这个(相同的3个数字)之外,我已经通过了所有测试用例。 我想在声明的数组中按比例分配certail现金。 我希望比例为Int或无小数。 请帮我解决这个测试用例:(
public class DistProp{
public static void main(String[] args){
double[] a={300,300,300};
double newA[]= new double[a.length];
double aTotal=0;
for(int i=0;i<a.length;i++){
aTotal+=a[i];
}
double cash=100;
System.out.println("cash:"+cash);
// this is just to show diff between running total and normal proporational distribution.
System.out.println("before");
double beforeTotal=0,afterTotal=0;
for(int i=0;i<a.length;i++){
double tmp =(a[i]/aTotal)*cash;
System.out.println("a["+a[i]+"]="+tmp);
beforeTotal+=tmp;
}
System.out.println("before Total:"+beforeTotal);
System.out.println("after");
double runningTotal = 0.0,oldRunningTotal=0.0;
for(int i=0;i<a.length;i++){
oldRunningTotal=runningTotal;
runningTotal+=(a[i]/aTotal)*cash;
newA[i]= (int)runningTotal - (int)oldRunningTotal;
}
for(int i=0;i<newA.length;i++){
double tmp =newA[i];
System.out.println("a["+a[i]+"]="+tmp);
afterTotal+=tmp;
}
System.out.println("after Total:"+afterTotal);
}
}
我得到的输出是:
cash:100.0
before
a[300.0]=33.33333333333333
a[300.0]=33.33333333333333
a[300.0]=33.33333333333333
before Total:99.99999999999999
after
a[300.0]=33.0
a[300.0]=33.0
a[300.0]=33.0
after Total:99.0
而不是总共100个。