2014-10-08 43 views
-5

我有一個整數數組列表。它將始終包含5個整數。現在假設它包含數字10031.現在我想在元素中進行這種計算。如何用數組列表元素進行計算。

添加此數字中的數字,直到您留下1或2位數字。

1 ..... 0 ..... 0 ..... ..... 3 1

... 1 ..... 0 ..... 0.3 .... 4

....... 1 ..... ..... 3 7

........... 4 ... 0.10(如果發生這種情況,分離爲1 + 0)

.............. 5..1 = 51結果是51.

我想要的結果是一個雙位數字。請幫忙。

+4

個人而言,我寧願你首先表現出一些努力;你可以發佈你有*不*工作?我也不清楚實際的計算規則是什麼。 – 2014-10-08 12:00:53

+1

@DaveNewton它就像一個向下的pascal三角形,除非你需要將兩位數字分成兩個一位數字,如果你有兩個或一個數字留下這個方案,然後將兩個數字合併成一個,你會得到一個數字 - 是我的猜測。 – EpicPandaForce 2014-10-08 12:03:35

+1

這基本上只是列表操作,如果你可以有第二個'臨時'列表,你計算出新的結果,然後替換原來的,那麼它實際上非常容易 - 你只需添加'i'和'i + 1 ',添加到另一個列表中,檢查數字是否大於10,將它們分成兩個數字(首先除以10,然後將剩餘部分作爲新元素添加到該元素之後),然後檢查是否有2個或更少列表中的元素,如果是,那麼只需將其中的一個數字設置爲5 * 10 + 1。這感覺就像是一個任務,我相信你可以做到這一點。 – EpicPandaForce 2014-10-08 12:07:16

回答

2
public static int yourFunction(ArrayList<Integer> list){ 
    String numbers = ""; // fill a string with your numbers 

    for(Integer i : list){ 
     numbers += String.valueOf(i); 
    } // could be nicer with java8 lambda function 


    String tmp_numbers; // temporary string needed 
    while(numbers.length() > 2){ 
     tmp_numbers = ""; 
     for(int i = 0; i < numbers.length()-1; ++i){ 
      // add two following numbers 
      // substring to read digit by digit 
      int v = Integer.parseInt(numbers.substring(i,i+1)); // first digit 
       v += Integer.parseInt(numbers.substring(i+1,i+2)); // + second 
      tmp_numbers = tmp_numbers + String.valueOf(v); // and fill the tmp string with it 
     } 
     numbers = tmp_numbers; // set the tmp string to our new beginning 
    } 
    return Integer.parseInt(numbers); 
} 
2

我認爲如果您自己解決了這個問題,那麼這對您可能會更好,但這是您的選擇。 評論中的所有解釋。

public static int countTriangle(List<Integer> list) { 
    if (list.size() == 0) { 
     // if list is empty return 0 
     return 0; 
    } else if (list.size() == 1) { 
     // if list contains only single element return this element 
     return list.get(0); 
    } else if (list.size() == 2) { 
     // if list contains two elements, return them connected 
     // for example we have list of 5 and 1 
     // we multiple 5 with 10 (50) and then add 1 to it, 
     // so the output of 5 and 1 will be 51 
     return list.get(0) * 10 + list.get(1); 
    } 
    // create new list for the next triangle's line 
    List<Integer> newList = new ArrayList<Integer>(); 
    // iterate over every element of existing list 
    for (int i = 0; i < list.size() - 1; i++) { 
     // a = current element + next element (last iteration will be with last but one element) 
     int a = list.get(i) + list.get(i+1); 
     // if a has two digits or more 
     if (a >= 10) { 
      // translate it to String 
      String s = String.valueOf(a); 
      // take every char of a String, translate it to number and add to new list 
      // for example if a = 157 then three new elements will be added to new list (1, 5, 7) 
      for (int j = 0; j < s.length(); j++) { 
       newList.add(Integer.valueOf(String.valueOf(s.charAt(j)))); 
      } 
     // if a has single digit 
     } else { 
      // add this to new list 
      newList.add(a); 
     } 
    } 
    // call this function with new list (next line) 
    return countTriangle(newList); 
} 
1
public void test() { 
    ArrayList<Integer> test = new ArrayList<Integer>(); 
    test.add(1); 
    test.add(0); 
    test.add(0); 
    test.add(3); 
    test.add(1); 
    ArrayList<Integer> ret = doTheMagic(test); 
    for (Integer i : ret) 
     System.out.println(i); 
    System.out.println(transformInDigit(ret)); 
} 

private int transformInDigit(final ArrayList<Integer> values) { 
    int ret = 0; 
    int size = values.size(); 
    for (int i = 0; i < values.size(); i++)   
     ret+=values.get(i)*Math.pow(10, size-i-1); 
    return ret; 
} 

private ArrayList<Integer> doTheMagic(final ArrayList<Integer> values) { 
    int size = values.size(); 
    if (size<=2) 
     return values; 
    ArrayList<Integer> ret = new ArrayList<Integer>(); 
    for (int i = 0; i < size - 1; i++) { 
     Integer newVal = values.get(i)+values.get(i+1); 
     if (newVal < 10) 
      ret.add(newVal); 
     else { 
      ret.add(newVal/10); 
      ret.add(newVal % 10); 
     } 
    } 
    return doTheMagic(ret); 
} 
0

因爲你始終相同的數字量,你可以做,沒有一個循環。 第一步,計算第一個元素+第二個元素,並將結果保存在新數組的第一個索引中。然後第二個+第三個和新數組的第二個索引中的結果。等等。然後,對新數組中的結果執行相同的操作,直至獲得結果。

此外,您必須在每次計算後計算交叉總和。爲此,您可以這樣做:

public int crossSum(int n) { 
    int sum = 0; 
     while(n > 0) { 
      int digit = n%10; 
      sum += digit; 
      n /= 10; 
     } 
    return sum; 
}` 

在最後一步中,您不計算交叉總和。當只有一個數字時,在前面顯示結果爲零。所以你得到一個兩位數的結果。