0

我正在尝试将向量拆分为 n 个部分。我检查了以下解决方案如何将向量拆分为 n 个“几乎相等”的部分

我根据此评论得出了以下代码: 要获得每个部分大小的基数,只需将总数除以部分数量:11/3 = 3。显然有些部分需要更大除了得到正确的总数之外,这只是余数:11 % 3 = 2。所以现在你知道其中 2 个部分的尺寸为 3+1,剩下的部分将是 3。(马克·兰森)

int main()
{
std::vector<int> lines;
        int size = 200;
        for(int i = 0; i < size;i++)
        {
            lines.push_back(i);
        }
        int p = 6;
        int right = round((double)size/(double)p);
        for(int i = 0; i < p;i++)
        {
            if( i < size - left)
            {
                vector<int> v;
                for(int j = 0; j < right; j++)
                {
                    v.push_back(lines[j]);
                }
                cout << v.size() << endl;

            }
            else if (i > size - left)
            {
                vector<int> v;
                for(int k = 0; k < right; k++)
                {
                    v.push_back(lines[k]);
                }
                cout << v.size() << endl;
            }
        }
   return 0;
}

p = 6 和 size = 200 的输出为:33,33,33,33,33,33 = 198

p = 6 和 size = 1000 的输出为:167,167,167,167,167,167 = 1002

两个输出都是错误的。我错过了什么?


编辑后:

所以让我理解。我们将i向右递增,这表示子向量的大小。虽然i小于size-right我们什么都不做。当 i 变得更大时,我们必须处理Leftovers,我们将块的大小更改为right = size - i

int main()
{
    std::vector<int> lines;
            int size = 1000;
            for(int i = 0; i < size;i++)
            {
                lines.push_back(i);
            }
            int p = 6;
            int right = round((double)size/(double)p);
            int left = size % p;
            for(int i = 0; i < size; i+= right)
            {
                if(i < size - right)
                {
                    vector<int> v;
                    //MAJOR CORRECTION
                    for(int j = i; j < (i+right); j++)
                    {
                        v.push_back(lines[j]);
                    }
                    cout << v.size() << endl;
    
                }
                else
                {
                    right = size - i;
                    vector<int> v;
                    //Major Correction
                    for(int k =i; k < size; k++)
                    {
                        v.push_back(lines[k]);
                    }
                    cout << v.size() << endl;
    
                }

return 0;
}

谢谢你。

输出:33 33 33 33 33 33 2 = 200

4

2 回答 2

1
int right = size/p; // don't round! this floors.
int left = size % p; // this one is correct.
for(int i = 0; i < p;i++)
        {
            if( i < size - left)
            {
                vector<int> v;
                for(int j = 0; j < right; j++) // counters, you used i here.
                {
                    v.push_back(lines[j]); // and here.
                }
                cout << v.size() << endl;

            }
            else if (i >= size - left)// sorry equal is here. try >= not > , comment with results.
            {
                vector<int> v;
                for(int j = 0; j < right+1; j++) // and here
                {
                    v.push_back(lines[j]); // and here
                }
                cout << v.size() << endl;
            }
        }
于 2013-11-06T22:20:15.890 回答
0

换个方式思考你的想法:
p (parts) = 3, size = 11, ceil(11/3) = 4所以 4+4+3 = 11

其他相同 p = 6, size = 200 ceil(200/6) = 34,34+34+34+34+34+30 = 200

int p = 6;
size_t nLimit = ceil((double)lines.size()/p);
// if you don't want to contain the leftover element within p elements, use floor
vector<int>::iterator start = lines.begin();
for(size_t i = 0; i < lines.size(); i+=nLimit){
    // Just use the constructor/insert function
    vector<int> v(start+i, start+std::min<size_t>(i+nLimit, lines.size()));
    cout<<v.size()<<endl;

}

这里的工作代码:http: //ideone.com/6V7rSX

于 2017-06-23T11:12:17.933 回答