2017-05-29 140 views
-1

讓我們假設我們的序列是如何獲得獨立序列的第n個元素?

3,9,7,1,然後重演3,9,7,1,等等...

我們如何檢查什麼樣的價值是要在例如第605個元素(在這種情況下它將是3)

PS:「獨立」我的意思是我們找不到數學公式。

在此先感謝:P

編輯添加無效代碼。

public static int determine_value_at_position_of_an_array(List<int> sequence, int power) 
    { 
     int counter = 1; /// This index is going to value of power. 
     int counter_2 = 0; /// This variable is responsible for jumping between [0..sequence.count-1] 
     int current_value = 0; 

     for (; counter<= power; counter++) 
     { 
      current_value = sequence[counter_2]; 
      if (counter_2==sequence.Count-1) /// if we're at last element, then go to 1st 
      { 
       counter_2 = 0; 
      } 
      else 
      { 
       counter_2++; 
      } 
     } 

     return current_value; 
    } 
+1

請告訴我們你一直在努力,實現這一目標是什麼。例如,一些代碼不需要工作。有關如何提問的更多信息,請查看[help-center](https://stackoverflow.com/help/how-to-ask) – Deathstorm

+0

@Deathstorm添加了代碼。 – Xiaox

回答

0

如果你知道你的序列重複的部分有多少元素具有(在您的示例4)你可以使用模運算符(大多%或MOD)接收的第N個元素。這裏:605%4 = 1 =>第一個元素=>三個。

編輯添加一些代碼

int valueAtN(List<Integer> sequence, int n) { 
    return sequence.get(n % sequence.size()); 
}