生成在递减级别

时间:2017-03-13 11:10:27

标签: java

目前,我需要在10到280之间生成100个增加的数字。增加应该在递减水平。以下是该系列中可能出现的一些数字的示例:

  

10,20,29,......,277.8,279,280

我真的不知道怎么做,这些数字并不重要,重要的是第一个值是10,最后一个值是280.其他98个数字应该增加到一个递减的水平

2 个答案:

答案 0 :(得分:1)

一个非常简单的例子如下:

public class Main {

    public static void main(String[] args) {
        double start = 10;
        double end = 280;
        int numberOfNumbers = 100;

        double[] list = new double[numberOfNumbers];
        list[numberOfNumbers - 1] = end;

        for (int i = 2; i < numberOfNumbers; ++i) {
            list[numberOfNumbers - i] = list[numberOfNumbers - (i - 1)] - (1.0/(numberOfNumbers - i));
        }

        list[0] = start;

        System.out.println(Arrays.toString(list));
    }
}

输出就像是 10, 274.83272349246164, 275.83272349246164, ..., 279.97948664001683, 279.98979591836735, 280

如果你的数字不是那么远,你需要选择一个收敛速度比0/1更快的函数,如1 /(n ^ 2)或1 /(2 * n)

答案 1 :(得分:0)

可能是这样的:

    double[] result = new double[100];
    result[0] = start;
    result[99] = 280;

    for (int i = 1; i < 99; i++) {
        result[i] = result[i - 1] + (280 - result[i - 1]) / 21.0;
    }

结果:
[10.0,22.857142857142858,35.102040816326536,46.76384839650146,57.870331806191864,68.44793505351606,78.52184290811053,88.11604086486717,97.25337225225445,105.95559262119471,114.24342154399497,122.13659194666188,129.65389709205894,136.81323532577042,143.63165269120992,150.12538351543802,156.30988906232193,162.19989434506851,167.80942318577954,173.15183160550433,178.23983962428983,183.0855615469427,187.7005348066121,192.09574743486866,196.28166422368443 ,200.26825164160422,204.0650015634326,207.6809538699358,211.12471797136743,214.4044933060642,217.5280888629183,220.50294177420793,223.33613502305516,226.0344143076716,228.60420410254437,231.05162295480417,233.38249805219445,235.60237909732805,237.71655152126482,239.73004906787125,241.647665778925,243.4739674085,245.2133022938095,246.86981170839002,248.44743972227621,249.94994259264402,251.38089770728,252.74371210217143,254.0416305734966,255.2777434033301 ,256.4549937174572,257.5761844928164, 258.64398523125374,259.66093831547977,260.6294650623617,261.5518714879635,262.4303537980605,263.26700361720043,264.0638129687623,264.82267901786884,265.5454085884465,266.23372246518716,266.8892594906544,267.5135804672899,268.1081718736094,268.67444940343756,269.2137613366072,269.7273917491497,270.2165635706188,270.68244149582745,271.1261347579309,271.54869976945804,271.9511426375791,272.33442155959915,272.69944910438016,273.04709438512396,273.3781851286895, 273.69350964637096,273.9938187108295,274.2798273436471,274.5522165177592,274.81163477881825,275.0586997893507,275.29399979938165,275.51809504703016,275.73151909240966,275.9347800880092,276.1283619885802,276.3127257034097,276.4883101937235,276.65553351783194,276.8147938265066,276.9664703109587,277.11092410567494,277.2484991482618,277.3795229983446,277.50430761747106,277.6231501118772,277.736333439883,280.0]