有序数列中,平衡选取一定数量的数

标题,不太好取… 其实就是选择的间隔step满足 总的数量/要选择的数量

实际业务场景是:要求在超过150张图片中,平衡的选取150张图片,用于合成视频。

为此写了以下 code :

import java.util.Arrays;
import java.math.*;
class Untitled {
    public static void main(String[] args) {
        int len = 340; //实际的数量
        int[] ary = new int[len];
        for (int i = 0; i < len; i++) {
            ary[i] = i;
        }
        //System.out.println("实际数据:" + ary);

        float rlen = 150f; //要选取的数量
        float step = len / rlen;
        //System.out.println("step==>" + step);

        int[] rary = new int[(int)rlen];
        for (int i = 0; i < rlen; i++) {
            int ri = Math.round(i * step);
            rary[i] = ary[ri];
        }
        System.out.println("选取后的数据:" + rary);
    }
}

输出:

选取后的数据:[0, 2, 5, 7, 9, 11, 14, 16, 18, 20, 23, 25, 27, 29, 32, 34, 36, 39, 41, 43, 45, 48, 50, 52, 54, 57, 59, 61, 63, 66, 68, 70, 73, 75, 77, 79, 82, 84, 86, 88, 91, 93, 95, 97, 100, 102, 104, 107, 109, 111, 113, 116, 118, 120, 122, 125, 127, 129, 131, 134, 136, 138, 141, 143, 145, 147, 150, 152, 154, 156, 159, 161, 163, 165, 168, 170, 172, 175, 177, 179, 181, 184, 186, 188, 190, 193, 195, 197, 199, 202, 204, 206, 209, 211, 213, 215, 218, 220, 222, 224, 227, 229, 231, 233, 236, 238, 240, 243, 245, 247, 249, 252, 254, 256, 258, 261, 263, 265, 267, 270, 272, 274, 277, 279, 281, 283, 286, 288, 290, 292, 295, 297, 299, 301, 304, 306, 308, 311, 313, 315, 317, 320, 322, 324, 326, 329, 331, 333, 335, 338]

猜你喜欢

转载自blog.csdn.net/jjwwmlp456/article/details/80271655
今日推荐