簡體   English   中英

C中數組中最長的遞增序列

[英]Longest increasing sequence in an array in C

我想制作一個程序,返回數組中最長的遞增序列。

例如:

輸入:1、2、3、2、6、2 輸出:1、2、3

輸入:4、3、1、2、4、6、4、1、5、3、7 輸出:1、2、4、6

我設法整理了一個代碼,但這只會返回第一個連續遞增的數字序列:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    int j = 0;
    int cou = 0; 
    int max = 0; 
    // c = final array; will contain the longest consecutive, increasing sequence of numbers
    int c[10];
    int n = 0;  
    int a[] = {1, 3, 5, 1, 5, 7, 8, 9, 10, 11, 12};

    for (int i = 0; i < (sizeof(a)/sizeof(int)); ++i) {
        if (a[i+1] > a[i])
            ++cou;
        if (cou > max) {
            max = cou;
            c[j] = a[i];
            c[j+1] = a[i+1];
            j++;
        }
        if (j > n)  //finding the size of my final array
            n = j;
        else {
            cou = 0;
            j = 0;
        }
    }

    for (j = 0; j <= n; ++j) 
        printf("%d ",c[j]);

    return 0;
}

所以基本上,我想要最長的遞增連續數字序列。

我在這個問題上絞盡腦汁已經有一段時間了,但仍然沒有設法打開它。 歡迎任何幫助。

您需要遍歷數組,查找序列並比較它們的長度。 因此,您需要記住要比較的序列的先前長度。 並且您無法即時將結果復制到輸出數組(如果您根本需要輸出數組),因為您無法預測下一個序列的長度。 我最好給你舉個例子。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
int previous_len=0, start=0, c[10], len=0;  //c = final array; will contain the longest consecutive, increasing sequence of numbers
int a[] = {1, 3, 5, 1, 5, 7, 8, 9, 10, 11, 12};
for (int i = 0; i < (sizeof(a)/sizeof(int)); ++i) {
    if(a[i+1] > a[i]) {
        len++;
        if (len > previous_len) { 
            previous_len=len;
            start=i+1-len;
        }
    } else {
        previous_len=len;       
        len=0;
    }
}
for(int i = 0; i <= previous_len; ++i) {
    c[i]=a[start+i]; //here you can copy data to output array, if you need it
    printf("%d ",c[i]); //you can output a[start+i] instead
}
return 0;

}

你似乎錯過了一些花括號:

if(a[i+1] > a[i])
{
    ++cou;
    if (cou>max)
        {max = cou;
        c[j]=a[i];
        c[j+1] = a[i+1];
        j++;}
    if (j > n)  //finding the size of my final array
        n=j;
}
else
    {cou = 0;
     j=0;}

我建議把它分解成更小的部分。

從一個函數開始:

int sequenceLength(int[] array, int arrayLen, int position)

...它返回從position開始的序列的長度。 測試它並確保它有效。 你不應該需要幫助來寫那個。

一旦你有了它,你可以寫這樣的東西:

int longestSequence(int[] array, int arrayLen) {
   int longest = 0;
   int longestLen = 0;
   for(int i=0; i<arrayLen; i++) {
       int seqLen = sequenceLength(array, arrayLen, i);
       if(seqLen > longestLen) {
           longest = i;
           longestLen = seqLen;
       }
   }
   return longest;
}

再次測試它並確保它適用於所有情況。

最后你需要一個函數:

printSequence(int[] array, int arrayLen, int position)

...打印從該位置開始的序列。 同樣,您應該能夠自己解決這個問題。

把所有這些放在一起:

printSequence(array,arrayLen(longestSequence(array,arrayLen)));

將這樣的挑戰分解成更小的部分來解決它總是最容易的。

可能有更有效的解決方案可以避免回溯,但以你的水平猜測,我認為你不需要去那里。

(注:雖然這里的代碼可以編譯,但請視為偽代碼)

您使用了一個數組來存儲最長的序列,這使您的代碼在打印時出錯。
並且您不能為導致錯誤序列的if()語句使用大括號。
您可以進行以下更改以使代碼工作,

int main()
{int j=0, cou=0, max=0, c[10], n=0;  //c = final array; will contain the longest consecutive, increasing sequence of numbers
int a[] = {1, 3, 5, 1, 5, 7, 8, 9, 10, 11, 12};
int i,k,z;
for ( k=0,i = 0; i < (sizeof(a)/sizeof(int)); ++i)
{if(a[i+1] > a[i])
  {  ++cou;
    if (cou>max)
        {max = cou;
        z=k;
        }

   }
    else
        {
            k=i+1;
            cou = 0;
         j=0;}
}
for( j = z; j <(sizeof(a)/sizeof(int)) ; ++j)
if(a[j]<a[j+1])
printf("%d ",a[j]);
else
 break;
printf("%d",a[j]);
return 0;

}

暫無
暫無

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

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