2

やあみんな、私はこれに3日間取り組んできましたが、私が見たすべてのものから何も思いつきませんでした.

とにかく配列を変更したり、新しい配列を作成したりせずに、約250個の浮動小数点数の配列を取り、K番目に大きい値を見つけようとしています。

他の関数ではデータを正しい順序で配置する必要があり、Arduino はメモリ空間にそれ以上の値を保持できないため、変更するか、新しいものを作成できます。

配列内の値は重複する可能性があります (おそらくそうなるでしょう)。

EG として : 配列がある場合 ::: 1,36,2,54,11,9,22,9,1,36,0,11; 最大から最小までは :: 1) 54 2) 36 3) 36 4) 22 5) 11 6) 11 7) 9 8) 9 9) 2 10) 1 11) 1 12) 0

どんな助けでも素晴らしいでしょう。私にとってこれをうまくやってくれる機能を求めるのは多すぎるかもしれません:)ハハハ

ここに私がこれまでに持っているコードがありますが、まだ重複を機能させようとさえしていません。何らかの理由で、何らかの理由で1つの答えしか得られません.2ですが、理由はわかりません

void setup()
{
  Serial.begin(9600);
}
void loop ()
{
 int Array[] = {1,2,3,4,5,6,7,8,9,10};

 int Kth = 6; //// just for testing putting the value as a constant
 int tr = 0;   /// traking threw the array to find the MAX

 for (int y=0;y<10;y++)  ////////////  finding the MAX first so I have somewhere to start
 {
   if (Array[y]>Array[tr])
   {
     tr = y;
   }
 }
 Serial.print("The max number is ");
 int F = Array[tr];
 Serial.println(F); // Prints the MAX ,,, mostly just for error checking this is done

 /////////////////////////////////////////////////////////  got MAX

 for ( int x = 1; x<Kth;x++)  //// run the below Kth times and each time lowering the "Max" making the loop run Kth times
 {
   for(int P=0;P<10;P++) // run threw every element
   {
   if (Array[P]<F)
   {
     for(int r=0;r<10;r++)    //and then test that element against every other element to make sure 
                             //its is bigger then all the rest but small then MAX
     {
       Serial.println(r);
       if(r=tr)  /////////////////// done so the max dosent clash with the number being tested
       {
       r++;
       Serial.println("Max's Placeing !!!!");
       }
     if(Array[P]>Array[r])
     {
       F=Array[P];            ////// if its bigger then all others and smaller then the MAx then make that the Max
       Serial.print(F);
       Serial.println(" on the ");
     }
}}}}
Serial.println(F); /// ment to give me the Kth largest number 
delay(1000);

}

4

1 に答える 1

1

速度が問題にならない場合は、次のアプローチを取ることができます (疑似コード):

current=inf,0

for i in [0,k):
    max=-inf,0
    for j in [0,n):
        item=x[j],j
        if item<current and item>max:
          max=item
    current=max

current次に、k 番目に大きいアイテムが含まれます。ここで、アイテムは値とインデックスのペアです。

考え方は単純です。最初に最大のアイテムを見つけるには、最大のアイテムを見つけるだけです。2 番目に大きいアイテムを見つけるには、1 番目に大きいアイテムよりも大きくない最大のアイテムを見つけます。3 番目に大きいアイテムを見つけるには、2 番目に大きいアイテムより大きくない最大のアイテムを見つけます。等

ここでの唯一の秘訣は、重複する可能性があるため、アイテムを一意にするために値とインデックスの両方を含める必要があることです。

Cで実装する方法は次のとおりです。

void loop()
{
  int array[] = {1,2,3,4,5,6,7,8,9,10};
  int n = 10;
  int k = 6; //// just for testing putting the value as a constant
  int c = n; // start with current index being past the end of the array
             // to indicate that there is no current index. 

  for (int x = 1; x<=k; x++) {
    int m = -1; // start with the max index being before the beginning of
                // the array to indicate there is no max index

    for (int p=0; p<n; p++) {
      int ap = array[p];
      // if this item is less than current
      if (c==n || ap<array[c] || (ap==array[c] && p<c)) {
        // if this item is greater than max
        if (m<0 || ap>array[m] || (ap==array[m] && p>m)) {
          // make this item be the new max
          m = p;
        }
      }
    }
    // update current to be the max
    c = m;
  }
  Serial.println(array[c]); /// ment to give me the Kth largest number 
  delay(1000);
}

C バージョンでは、配列を参照することで現在の値と最大値をいつでも取得できるため、現在のインデックスと最大インデックスを追跡するだけです。

于 2013-01-13T18:32:30.167 に答える