簡體   English   中英

C ++ | 需要幫助從For循環功能->遞歸功能過渡

[英]C++ | Need Help Transitioning From A For Loop Function -> a Recursive Function

因此,對於我的一項家庭作業,我們被要求創建一個冰雹序列,而我完全陷入困境。 我很難弄清楚如何分解包含循環的函數的各個部分,然后嘗試從本質上將其轉換為遞歸函數。 任何幫助將不勝感激。 下面,我擁有原始的for循環功能,然后是(嘗試)遞歸功能。

// startingValue function with For-Loop
// Takes in an int value n and returns the
// start value k from 1 to n, that has the longest length.

int startingValue (int n) {
    int length = 0;
    int k;

    for ( int i = 1; i <= n; i++ ) {
        int currentLength = largestHailstoneLength(i);

        if (currentLength > length) {
            length = currentLength;
            k = i;
        }
    }       

    return k;
}

這是我對上述函數的遞歸版本的嘗試:

int startingValue (int n) {
    int length = 0;
    int n = 0;
    int currentLength = largestHailstoneLength(i);

    if (currentLength < length) {
        return 0;
    } else {
        length = currentLength;
        n = i;
    }

    return (startingValue(i));
}

這是一些簡單但可能效率不高的方法,應該可以幫助您入門:

int startingValue(int n) {

    if (n == 1) {
        return n;
    }

    int m = startingValue(n - 1);

    return largestHailstoneLength(n) > largestHailstoneLength(m) ? n : m;
}

我不會做家庭作業,但將向您展示Ruby(一種與偽代碼一樣可讀的語言)中的解決方案。 #之后的所有內容均為注釋。

# Ruby equivalent to your C++ loop version
def start(n)
  biggest = 0
  k = -1
  (1..n).each do |i|  # loop from 1 to n, inclusive...
    # don't have access to your largestHailstoneLength(i) function,
    # so I'm using a random number on the range [0,1000)
    current = rand(1000)
    biggest, k = current, i if current > biggest
  end
  return k
end

# Using default argument values.  Alternatively, you can build
# a single-argument front end function and a recursive back end
# with these args the state info.
def start_recursive(n, i = 1, biggest = 0, k = -1)
  current = rand(1000)  # same as above
  biggest, k = current, i if current > biggest
  return i < n ? start_recursive(n, i + 1, biggest, k) : k
end

srand 12345  # explicit seed to RNG for reproducibility of sequence
p start(1000)  # => 301
srand 12345  # reseed RNG sequence to show recursive version yields same result
p start_recursive(1000)  # => 301

附錄

在考慮了一下並擴大了范圍之后,我想到了以下變種,該變種不太可能遭受堆棧溢出的困擾,它使用分而治之的策略來划分候選范圍。 我還用調用您的largestHailstoneLength(i)代替了隨機數評估:

def start_recursive(upper_index, lower_index = 1)
  return lower_index if lower_index >= upper_index
  mid = (lower_index + upper_index) / 2
  lower_max = start_recursive(mid, lower_index)
  upper_max = start_recursive(upper_index, mid + 1)
  if largestHailstoneLength(lower_max) < largestHailstoneLength(upper_max)
    return upper_max
  else
    return lower_max
  end
end

您的代碼中有一些問題

  1. 您沒有在遞歸函數中定義i
  2. 您似乎對遞歸的工作方式一無所知,我給您一個線索:
    • 每次調用函數時,都會保存一個狀態。
    • 您需要聲明遞歸函數停止的位置,否則它將繼續調用自身,直到由於堆棧溢出而崩潰為止。
    • 您需要知道您是否要使用該函數返回的內容,還是要使用傳遞給它的內容(或兩者都使用)

如果我做對, startingValue()應該返回一個值,其中largestHailstoneLength()是最大可能值。

因此,我們遵循的是歸納原理:

  • 我有n
  • 如果n1 ,我不必進行任何比較,只需返回n

除此以外

  • 如果n最大,則它應大於1n - 1之間的最大值。
  • 現在我們需要看到1n - 1之間的最大值。
  • 如果n - 1最大,則它應大於1n - 2之間的最大值
  • ...

好吧,首先要做的是檢查n

// Stop here, no need to go compare
if (n == 1) return n;

遞歸內需要做的是比較當前 n1n - 1的最大值。 那是:

// If current n is greater then the best case for n - 1
int k = startingValue(n - 1);
if (largestHailstoneLength(n) > largestHailstoneLength(k))
{
    // n is greater
}
else // startingValue(n - 1) is greater

知道哪個更大,我們簡單地將其返回。

為了更好地理解事物,請多讀一些。 您可以從這里開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM