// Say you have the following typedef enum { red, yellow, blue } Color; const Color expectedColors[] = {Red, Yellow, Blue}; // The main loop should be something like: for (int i = 0; i < 3N; i++) { Color expectedColor = expectedColors[i%3]; if (getColor(i) != expectedColor) { exchange(i, findNext(i+1, expectedColor)); } } int findNext(int start, Color expectedColor) { while(getColor[start++] != expectedColor); return start; }
【在 a**********s 的大作中提到】 : // Say you have the following : typedef enum { : red, : yellow, : blue : } Color; : const Color expectedColors[] = {Red, Yellow, Blue}; : // The main loop should be something like: : for (int i = 0; i < 3N; i++) { : Color expectedColor = expectedColors[i%3];
【在 a**********s 的大作中提到】 : // Say you have the following : typedef enum { : red, : yellow, : blue : } Color; : const Color expectedColors[] = {Red, Yellow, Blue}; : // The main loop should be something like: : for (int i = 0; i < 3N; i++) { : Color expectedColor = expectedColors[i%3];
这个很简单,findNext(i, expectedColor)的调用稍微改变一下: // The start indices where we should look for a particular color: int lookupIndices[] = {-1, -1, -1}; // The main loop should be something like: for (int i = 0; i < 3N; i++) { Color expectedColor = expectedColors[i%3]; if (getColor(i) != expectedColor) { int indexOfExpected = findNext( max(lookupIndices[expectedColor], i+1), expectedColor); exchange(i, indexOfExpected); lookupIndices[expectedColor] = indexOfExpected + 1; } }
【在 a**********s 的大作中提到】 : 这个很简单,findNext(i, expectedColor)的调用稍微改变一下: : // The start indices where we should look for a particular color: : int lookupIndices[] = {-1, -1, -1}; : // The main loop should be something like: : for (int i = 0; i < 3N; i++) { : Color expectedColor = expectedColors[i%3]; : if (getColor(i) != expectedColor) { : int indexOfExpected = findNext( : max(lookupIndices[expectedColor], i+1), : expectedColor);