0

一連の傾向線に一連の重みを割り当てようとしています。重みは配列にあり、傾向線は配列にあります。私はすべての順列で終わりたいと思っています。つまり、配列内の各トレンドラインにそれぞれの重みが割り当てられます。私のコードでは、現在この出力を取得しています:

output: 1 combination per line:
1.00, 1.00, 1.00, 1.00 
1.50, 1.50, 1.50, 1.50 
2.00, 2.00, 2.00, 2.00, 

しかし、私が取得しようとしているのは、重複を除くすべての順列です。つまり、重み/トレンドラインのさまざまな可能な組み合わせをすべて取得したいのです。

output: 1 combination per line:
1.00, 1.00, 1.00, 1.00
1.50, 1.00, 1.00, 1.00, 
2.50, 1.00, 1.00, 1.00, 
1.00, 1.50, 1.00, 1.00, 
1.50, 1.50, 1.00, 1.00, 
2.50, 1.50, 1.00, 1.00, 
....etc

編集:
私の質問は、次のコードを変更して、トレンドラインと重みのさまざまな組み合わせをすべて生成できるようにする方法です。たとえば、2 つの傾向線と 3 つの重み (1、2、3) がある場合、すべての組み合わせは次のようになります。

1,1
1,2
1,3
2,1
2,2
2,3
3,1
3,2
3,3

コードは次のとおりです。

#define TRENDLINE_COUNT 4
#define WEIGHT_COUNT 3
void hhour(void)
{   
    int trendLine[TRENDLINE_COUNT];
    double weight[] = { 1, 1.5, 2 };
    FILE *myFile = fopen("s:\\data\\testdump\\ww.txt", WRITE);

    fprintf(myFile, "output: 1 combination per line :\n"    );
    for (int weightIndex = 0; wei`enter code here`ghtIndex < WEIGHT_COUNT; weightIndex++)
    {
        double currentWeight = weight[weightIndex];
        double cumulativeTrendValue = 0;

        for (int trendLineIndex = 0; trendLineIndex < TRENDLINE_COUNT; trendLineIndex++)
        {
            cumulativeTrendValue += trendLine[trendLineIndex] * currentWeight;

            fprintf(myFile, "%.02lf, ", currentWeight);
        }
        fprintf(myFile, "\n");

        // do something with cumulativeTrendValue
    }

    fclose(myFile);
}
4

1 に答える 1

0

これがあなたの望むものだと思います。よりクリーンなソリューションはおそらく存在しますが、私は可能であればブルート フォーシング ソリューションの大ファンです。トレンドラインを変更したい場合...ええと、おそらく再帰的な可変関数を備えたもの...しかし、そのレベルの狂気に直面すると充電を開始します。

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

#define TRENDLINE_COUNT 4
#define WEIGHT_COUNT 3

double weight[] = { 1, 1.5, 2 };

int addOne(int counter[TRENDLINE_COUNT])
{
  int i=0;
  while(1)
  {
    if(counter[i]+1 >= WEIGHT_COUNT)
    {
      if( i+1 >= TRENDLINE_COUNT)
      {  return 1;}
      else
      { 
        counter[i]=0;
        i++;
      }
    }
    else
    {
      counter[i]++;
      return 0;
    }
  }
  return 0;
}

void hhr()
{
  int i;
  int counter[TRENDLINE_COUNT];  

  memset(counter, 0, sizeof(int)*TRENDLINE_COUNT);
  do 
  {
    for(i=0; i<TRENDLINE_COUNT; i++)
    { 
      //printf("%d ", counter[i]);
      printf("%f ", weight[counter[i]]);
    }
    printf("\n");
  }while(!addOne(counter));
}

int main(int argc, char **argv)
{
  hhr();
}
于 2013-02-11T22:00:55.443 に答える