如何在java中从2 ^ n更改为(2 ^ n + 1)?

时间:2017-04-13 15:39:03

标签: java loops methods stack

我正在使用链接列表和调整数组大小进行一些测试,我需要测试功率为2和2加1的功率(1,2,3,4,5,8,9,16,17) )

我已经为自己构建了一个代码,它可以很好地处理2的幂,但不是2加1的功能

这是我的代码

public static void testsForLinkedLists(int maxPowerOf,
        int intendedRepeats, String excelFile,
         IntToDoubleFunction pushMethod, IntToDoubleFunction  popMethod) {


        ArrayList<Double> listOfPushTimes = new ArrayList<Double>();
        ArrayList<Double> listOfPopTimes = new ArrayList<Double>();


        double maxPush = 0;
        double minPush = 100;
        double maxPop = 0;
        double minPop = 100;
        double medianPush = 0;
        double medianPop = 0;
        double sumPush = 0;
        double sumPop = 0;
        double averagePush = 0;
        double averagePop = 0;



        int total = 0;
        int excelLine = 1;
        //int actualPowerOf = 0;
        for (int actualPowerOf = 0; actualPowerOf < maxPowerOf; actualPowerOf++) {

            listOfPushTimes.clear();
            listOfPopTimes.clear();
            //actualPowerOf++;
            maxPush = 0;
            minPush = 100;
            maxPop = 0;
            minPop = 100;
            medianPush = 0;
            medianPop = 0;
            sumPush = 0;
            sumPop = 0;
            averagePush = 0;
            averagePop = 0;
            total = 0;
            int powerOf2 = (int)Math.pow(2, actualPowerOf);
            for (int i = 0; i < intendedRepeats; i++) {

                double timetopush = pushMethod.applyAsDouble(powerOf2);
                listOfPushTimes.add(timetopush);
                sumPush = sumPush + timetopush;
                if (timetopush > maxPush) {
                    maxPush = timetopush;
                }
                if (timetopush < minPush) {
                    minPush = timetopush;
                }
                double timetopop = pushMethod.applyAsDouble(powerOf2);
                listOfPopTimes.add(timetopop);
                sumPop = sumPop + timetopop;
                if (timetopop > maxPop) {
                    maxPop = timetopop;
                }
                if (timetopop < minPop) {
                    minPop = timetopop;
                }
                total++;
            }
            averagePush = sumPush / total;
            averagePop = sumPop / total;
            medianPush = measuring_tools.medianOf(listOfPushTimes);
            medianPop = measuring_tools.medianOf(listOfPopTimes);

}

如何编辑我的代码,以便执行2加1的功率测试?

1 个答案:

答案 0 :(得分:0)

要生成一系列您需要的内容,您可以使用以下伪代码:

int v=1;
for(int i=0; i<n;i+=2) {
    write(v, v+1);
    v=2*v;
}