2017-04-13 30 views
-6

我正在做一些鏈接列表測試和調整數組大小,我需要做2次冪和2加1(1,2,3,4,5,8,9,16,17)如何在java中從2^n改爲(2^n + 1)?

我已經建立自己說,是很好的做了兩個功率代碼,但不fr的二加一

功率這裏是我的代碼

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到變量? –

+0

是的,我該怎麼辦呢? –

+6

你寫了所有的,你不知道如何將1添加到變量?這似乎並不可疑。 – Shadov

回答

0

產生一系列的什麼ü需要,ü可以使用下面的僞代碼:

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