2016-11-20 1 views
-2

私はサイズnの配列を持ち、k個のサブ配列に分割したいと思います。各配列はほぼ同じサイズでなければなりません。私はしばらく考えており、2つのforループを使用する必要があることを知っていますが、これらのforループを実装するのは苦労しています。nの長さの配列を、ほぼ同じサイズ(k)のk個のサブ配列に分割する方法は?

//Lets call the original integer array with size n: arr 
// n is the size of arr 
// k is the number of subarrays wanted 

int size_of_subArray = n/k; 
int left_over = n%k; // When n is not divisible by k 
int list_of_subArrays[k][size_of_subArray + 1]; 

//Lets call the original integer array with size n: arr 

for(int i = 0; i < k; i++){ 
    for(int j = 0; j < size_of_subArray; j++){ 
     list_of_subArrays[i][j] = arr[j]; 
    } 
} 

を私はforloopsに正しいインデックスを取得して苦労しています:私が試した何

すべてのアイデア?

+0

私は自分の投稿を編集しました。 – Mat

+0

@Mat "n"とは何か "k"とは "arr"とは何ですか? – Stargateur

+0

nは元の配列(arrと呼ばれる)のサイズであり、kは必要とされる部分配列の数です。 – Mat

答えて

0

私はあなたのコードをリファクタリングし、注釈を付けました。

主なポイントは、次のとおり、サブ配列サイズを計算するとき、0からインクリメントし続けることarrニーズに

  • インデックスを切り上げなければならない

    1. (すなわち、それがリセットしません

  • )0に次の動作するはずですが、私はそれをテストしていない[無償スタイルのクリーンアップをご容赦ください]:

    // Lets call the original integer array with size n: arr 
    // n is the size of arr 
    // k is the number of subarrays wanted 
    
    // round up the size of the subarray 
    int subsize = (n + (k - 1))/k; 
    
    int list_of_subArrays[k][subsize]; 
    
    int arridx = 0; 
    int subno = 0; 
    
    // process all elements in original array 
    while (1) { 
        // get number of remaining elements to process in arr 
        int remain = n - arridx; 
    
        // stop when done 
        if (remain <= 0) 
         break; 
    
        // clip remaining count to amount per sub-array 
        if (remain > subsize) 
         remain = subsize; 
    
        // fill next sub-array 
        for (int subidx = 0; subidx < remain; ++subidx, ++arridx) 
         list_of_subArrays[subno][subidx] = arr[arridx]; 
    
        // advance to next sub-array 
        ++subno; 
    } 
    

    UPDATE:

    はいこれは、n個のサブアレイにアレイを分割し、それは均等に分割しません。サイズ10の配列があり、それを9つのサブ配列に分割したいとします。次に、8つのサブアレイに元の配列の要素が1つありますが、1つのサブアレイには2つの要素が必要です。

    元のコードにはいくつかのバグがありました[上記の例では修正済み]。私が自分のためにこれをやっていたとしても、上記のことは何かを働かせるための第一歩でした。あなたの元の質問に

    、あなたは言った:「と、それぞれのアレイは同じサイズでなければなりません」。しかし、ここでは、リストサブアレイの物理的なサイズがあります[それでも切り上げ値]。

    しかし、私は、あなたの意図をさらに明確にするために、「均等に配分された」、またはそのようなものを言っている可能性があります。つまり、ではない最後のサブアレイ/バケットをではなく「短く」したいと考えています。

    このコードでは、やや同じように始まりますが、もう少し洗練されている必要があります。これはまだ少し粗いので、さらに最適化されるかもしれません:

    #include <stdio.h> 
    
    #ifdef DEBUG 
    #define dbgprt(_fmt...)  printf(_fmt) 
    #else 
    #define dbgprt(_fmt...)  /**/ 
    #endif 
    
    int arr[5000]; 
    
    // Lets call the original integer array with size n: arr 
    // n is the size of arr 
    // k is the number of subarrays wanted 
    
    void 
    fnc2(int n,int k) 
    { 
        // round up the size of the subarray 
        int subsize = (n + (k - 1))/k; 
    
        int list_of_subArrays[k][subsize]; 
    
        dbgprt("n=%d k=%d subsize=%d\n",n,k,subsize); 
    
        int arridx = 0; 
    
        for (int subno = 0; subno < k; ++subno) { 
         // get remaining number of sub-arrays 
         int remsub = k - subno; 
    
         // get remaining number of elements 
         int remain = n - arridx; 
    
         // get maximum bucket size 
         int curcnt = subsize; 
    
         // get projected remaining size for using this bucket size 
         int curtot = remsub * curcnt; 
    
         // if we're too low, up it 
         if (curtot < remain) 
          ++curcnt; 
    
         // if we're too high, lower it 
         if (curtot > remain) 
          --curcnt; 
    
         // each bucket must have at least one 
         if (curcnt < 1) 
          curcnt = 1; 
    
         // each bucket can have no more than the maximum 
         if (curcnt > subsize) 
          curcnt = subsize; 
    
         // last bucket is the remainder 
         if (curcnt > remain) 
          curcnt = remain; 
    
         dbgprt(" list[%d][%d] --> arr[%d] remain=%d\n", 
          subno,curcnt,arridx,remain); 
    
         // fill next sub-array 
         for (int subidx = 0; subidx < curcnt; ++subidx, ++arridx) 
          list_of_subArrays[subno][subidx] = arr[arridx]; 
        } 
    
        dbgprt("\n"); 
    } 
    
    +0

    はい、これは配列をn個のサブ配列に分割しますが、均等には分割しません。サイズ10の配列があり、それを9つのサブ配列に分割したいとします。次に、8つのサブアレイに元の配列の要素が1つありますが、1つのサブアレイには2つの要素が必要です。 – Mat

    関連する問題