2017-06-21 62 views
-1
  1. 我有兩個列表,一個用於正數,另一個用於負數(我將它轉換爲正數以輕鬆減法)。 我試圖用正值減去這些值。添加多個正值並用匹配的數字減去它

  2. 到目前爲止,我實現使零值,其中兩個數字是在列表中相等(例如:10和-10,或20 -20)。

  3. 但是,也有肯定列表一些值(例如:3,2,2,1),它可以匹配爲負值(如:-8)在列表中,使所有的值爲零。這是我無法弄清楚的部分。請儘量幫忙!
  4. 如果輸入(再次獲得從Excel工作表這些值並將其存儲在一兩個單獨的列表,一個用於正和負其他)爲:POS = [10,20,46,5,3,2,2,1 ]和neg = [ - 10,-20,-8,-46]。
  5. 輸出應該是[0,0,0,5,0,0,0,0],因爲所有的值都發現比賽在否定清單後變爲零。

    for (int b = 0; b < negativeValues.size(); b++) { 
        for (int e = 0; e < positiveValues.size(); e++) { 
         pvalues = positiveValues.get(e); 
         nvalues = negativeValues.get(b); 
         if (pvalues == nvalues) { 
          result = pvalues - nvalues; 
          positiveValues.set(e, 0.0); 
          negativeValues.set(b, 0.0); 
          // System.out.println(pvalues+" "+nvalues+" "+positiveValues+" "+negativeValues); 
          continue; 
         } 
         if (positiveValues.get(e) != 0 && negativeValues.get(b) != 0 
           && positiveValues.get(e) <= negativeValues.get(b)) { 
          max = Collections.max(negativeValues); 
          posVal1 = positiveValues.get(e); 
          indexOfPos = positiveValues.indexOf(posVal1); 
          // System.out.println(posVal1+ " "+max+" "+indexOfPos); 
          difference = max - posVal1; 
          System.out.println(difference + " " + posVal1); 
          while (difference <= 0) { 
           posVal1 = positiveValues.get(e); 
    
          } 
         } 
        } 
    } 
    

這裏是僞代碼。

  i) one list for negative numbers and sorted 
     ii) one list for postivie numbers and sorted 
     iii)iterate and start with the maximum number in negative list 
     iv) check the index of the equal or lesser number in the positive list 
     v) iterate the positive list using from this index: 
      a) if current positive number 
      1) equal to negative number - nullify and put them in map, go  to next negative number 
      2) else if less than negative number - 
       i) get the difference of this positive number and the negative number 
       ii) create a variable for this difference and put it in while loop 
       iii) search for the index of the number equal to or lesser to this difference. 
       iv) get the second positive number and if is 
       a) equal to difference, nullify all by updatng index of the negative and two positives numbers as NA 
       update difference to 0 and exit while and go to next negative number 
       b) else if the second positive number is less than the difference, sum the two numbers and negative number 
       and get the difference and update the difference variable with this new value and iterate while loop 
       c) if there is no lesser or mathcing number equal to difference,         come out of while and pick the next element in positive list as done in step (v) and get next number and repeat further. 
+0

請,您的問題提供例如輸入和輸出。 – Nurjan

+0

@Nurzhan Look,它的'簡單,只需找到可能的值,可以加上負數,然後使它們全部爲零。 –

+0

如果'pos = [8,15]'和'neg = [3,5,10]'會怎麼樣? –

回答

0

我認爲一個解決方案並不簡單。 您想查找組合的總和。爲此,你必須使用遞歸函數。

例如,我的解決方案:

static boolean find; 
static boolean[] flag; 

public static void findNumber(List<Integer> list, int sumNumber, int targetNumber) { 
    if (sumNumber == targetNumber) { 
     System.out.println("Find Number"); 
     find = true; 
     return; 
    } 

    for (int i = 0; i < list.size(); i++) { 
     if (flag[i] == false && list.get(i) + sumNumber <= targetNumber) { 
      flag[i] = true; 
      findNumber(list, sumNumber + list.get(i), targetNumber); 
      if (find) 
       return; 
      flag[i] = false; 
     } 
    } 
} 

public static void main(String[] args) { 
    List<Integer> positiveList = new ArrayList<Integer>(); 
    List<Integer> negativeList = new ArrayList<Integer>(); 

    for (int i = 0; i < negativeList.size(); i++) { 
     flag = new boolean[positiveList.size()]; 
     find = false; 
     findNumber(positiveList, 0, negativeList.get(i)); 

     if (find) { 
      negativeList.set(i, 0); 
      for (int j = 0; j < positiveList.size(); j++) { 
       if (flag[j]) 
        positiveList.set(j, 0); 
      } 
     } 
    } 

    System.out.println("PositiveList: " + positiveList); 
    System.out.println("NegativeList: " + negativeList); 

} 

我不知道這個解決方案,你想要的。