1

int 配列から最小公倍数を見つけなければなりません。コードを書きましたが、正しく動作しません。

これが私のロジックです。 1.配列をソートします 2.更新された最小共通カウンターを取得します 3.すべてが一意であるかどうかを取得します

そして以下のコード、

static int min_loc ; //minimum value location 
static int min_cnt ;
int all_uniqFlag = true;

void leastCommon(int data[],int n)
{
   int rcount = 0; //Repeated number counter
   int mcount = n; // minimum repetetion counter;

   // The array is already sorted we need to only find the least common value. 
   for(int i = 0 ; i < n-1 ; i++)
   {  
      //Case A : 1 1 2 2 2 3 3 3 3 4 5 5 5 5 : result should be 4 
      //Case B : 1 2 3 4 5 6 7 (All unique number and common values so all values should be printed
      //                        and ) 
      //Case C : 1 1 2 2 3 3 4 4 (all numbers have same frequency so need to display all )
      cout << "data[i] : " << data[i] << " data[i+1] : " << data[i+1] <<  "i = " << i << endl;
      if(data[i] != data[i+1])
      {
         //mcount = 0;
         //min_loc = i; 
         //return;
      }
      if(data[i] == data[i+1])
      {
        all_uniqFlag = false;
        rcount++;
      }
      else if(rcount < mcount)
      {
         mcount = rcount;
         min_loc = i ;//data[i];
      }
   } 
   min_cnt = mcount;   
}

コメントで述べたように、ケース B のみが機能し、ケース A と C は機能しません。問題の解決を手伝っていただけますか?

4

3 に答える 3

1

アプローチは-

  • ソートされた配列から最初の要素を選択し、それに連続する要素が同じである間、ループが中断されるまでそれらを output[] に格納します
  • 要素の頻度を leastFrequency に格納します
  • 次の要素を選択し、連続する要素をチェックして、ループが中断するまで同じ output[] に保存します
  • この頻度を leastFrequency で確認します
    • 同じ場合、何もしない (これらを出力 [] に追加する)
    • 少ない場合は、output[] をクリアし、同じ番号の要素を保存します。の回
    • それ以上の場合、この要素を反復する前に有効な出力[]の長さを以前の長さに変更します
  • 同様に、すべての個別の要素に対して反復し、最終的に output[] から 0 から有効な長さまでの結果を取得します

    void leastCommon(int data[], int len) {
    
    if ( len > 0) {
        int output[] = new int[len];
        int outlen = 0; // stores the size of useful-output array
        int leastFrequency = len; // stores the lowest frequency of elements
    
        int i=0;
        int now = data[i];
        while (i < len) {
            int num = now;
            int count = 0;
            do {
                output[outlen] = now;
                outlen++;
                count++;
    
                if((++i == len)){
                    break;
                }
                now = data[i];
            } while (num == now);   // while now and next are same it adds them to output[] 
    
            if (i - count == 0) { // avoids copy of same values to output[] for 1st iteration
                leastFrequency = count;
            } else if (count < leastFrequency) {  // if count for the element is less than the current minimum then re-creates the output[]
                leastFrequency = count;
                output = new int[len];
                outlen = 0;
                for (; outlen < leastFrequency; outlen++) {    
                    output[outlen] = num;   // populates the output[] with lower frequent element, to its count 
                }
            } else if (count > leastFrequency) {
                outlen -= count;    // marks outlen to its same frequent numbers, i.e., discarding higher frequency values from output[]
            }
        }
        //for(int j = 0; j < outlen; j++) {
        // print output[] to console
        //}
      }
    }
    

改善を提案してください。

于 2014-04-24T18:14:31.257 に答える
1
  • リストをスキャンする
  • リスト内の各要素をout配列内の最後の要素と比較します
  • 要素が一致する場合、そのカウントを 1 増やします
  • 要素が一致しない場合は、新しい要素をout 配列に追加し、 index1ずつ増やします

スキャンが完了すると、out配列にはすべての個別の要素out[][0]とその頻度が含まれますout[][1]

  • 頻度リスト ( ) をスキャンしてout[][1]、最も低い頻度を見つけます。
  • 最後に、要素リストout[][0]をもう一度スキャンして、頻度が最も低い要素と一致する要素を出力します

.

#include<stdio.h>
#include<stdlib.h>
#define N 8
int main()
{
    //int data[N]={1,2,3,4,5,6,7};
    int data[N]={1,1,2,2,3,3,4,4};
    //int data[N]={1,1,2,2,2,3,3,3,3,4,5,5,5,5};
    int out[N][2];
    int i=0,index=0;
    for(i=0;i<N;i++)
    {
        out[i][0]=0; 
        out[i][1]=0; 
    }
    out[0][0] = data[0];
    out[0][1]=1;
    for(i=1;i<N;i++)
    {
        if(data[i] != out[index][0])
        {
            index++;
            out[index][0] = data[i];
            out[index][1] = 1;
        }
        else
        {
            out[index][1]++;
        }
    }

    int min=65536;
    for(i=0;i<N;i++)
    {
        if(out[i][1] == 0)
        {
            break;
        }
        if(out[i][1] < min)
        {
            min = out[i][1];
        }
    }
    for(i=0;i<N;i++)
    {
        if(out[i][1] == min)
        {
            printf("%d\t",out[i][0]);
        }
    }
    printf("\n");
}
于 2014-04-24T02:58:32.140 に答える