【发布时间】:2018-06-13 10:18:47
【问题描述】:
我有这个可行的算法,但我想将其更改为具有动态比率,以便使第一个位置的值的距离高于最后一个位置的值。
例如,如果奖金 = 1000,total_prizes = 5,last_prize = 50 我得到了:
1) 350.00
2) 275.00
3) 200.00
4) 125.00
5) 50.00
TOTAL SUM: 1000.00
我希望看到的应该是:
1) 475.00
2) 250.00
3) 150.00
4) 75.00
5) 50.00
TOTAL SUM: 1000.00
而不是在位置之间始终保持固定值 75。在这种情况下,增加接近第一个位置的距离 从 5) 到 4) 25 从 4) 到 3) 75 从 3) 到 2) 100 从 2) 到 1) 225
这里是当前代码:
public static void main(String[] aa){
float ratio;
float first_prize;
float s=0;
float money = 1000;
int total_prizes = 5;
float last_prize = 50;
float prizes[] = new float[total_prizes+1];
first_prize=2*(money/total_prizes)-last_prize; //last member of the progresion
ratio=(first_prize-last_prize)/(total_prizes-1);
prizes[total_prizes]=last_prize;
for (int j = total_prizes-1; j >=1; j--) {
prizes[j]=prizes[j+1]+ratio;
}
for(int k=1;k<=total_prizes;k++){
System.out.printf("%d) %.2f\n",k,prizes[k]);
s+=prizes[k];
}
System.out.printf("TOTAL SUM: %.2f\n",s);
}
循环内部:
for (int j = total_prizes-1; j >=1; j--) {
//ratio here should be calculated dynamically based on position...
prizes[j]=prizes[j+1]+ratio;
}
谢谢! :)
【问题讨论】:
标签: java algorithm math statistics game-engine