如何使用 C++ 实现分配策略

how do you implement allocating strategy with c++

本文关键字:分配 策略 实现 C++ 何使用      更新时间:2023-10-16

我有两个工人。如果我配置了一个策略,即 60% 的任务分配给 A 工作人员,其余分配给 B 工作人员。

如何使用 C++ 实现。

你的建议是什么?

map<string,float> m_percent;
m_percent.insert(make_pair("countA",0.6));
m_percent.insert(make_pair("countB",0.1));
m_percent.insert(make_pair("countC",0.3));
map<string,int> m_count;
m_count.insert(make_pair("total",0));
map<string,int>::iterator it = m_count.find("countA");
map<string,int>::iterator itc =m_count.find("total");
map<string,float>::iterator itp=m_percent.find("countA");
if(it== m_count.end())//use countA
{      
    m_count.insert(make_pair("countA",1));
 }     
else
{
    int &c = it->second; 
    if(itc!=m_count.end()&&itp!=m_percent.end())
    {
        float f=(c+1)*100/(itc->second+1)*100.0
        if (f<=itp->second)
        {
            it->second=it->second+1;

        }   
    }
}   

if(itc!=m_count.end())
{
   itc->second=itc->second+1;    
}

如果您谈论的是任务数量而不考虑复杂性,只需计算分配给每个任务的作业数量即可。让我们将这些计数称为分配给A的作业的countAcount作业总数(为了简化计算),并将它们初始化为零。

然后,当作业进入时,按以下方式分配:

  • 如果 count 等于零,则将其分配给 A 并递增 countAcount
  • 否则,如果countA / count小于 0.6 ,则将其分配给 A 并递增 countAcount
  • 否则将其分配给B并仅递增count.

从长远来看,这将倾向于平均分配,以便A得到 60%:

countA  count  countA/count  allocateTo
------  -----  ------------  ----------
     0      0             ?      A
     1      1         1.000      B
     1      2         0.500      A
     2      3         0.667      B
     2      4         0.500      A
     3      5         0.600      B
     3      6         0.500      A
     4      7         0.571      A
     5      8         0.625      B
     5      9         0.556      A
     6     10         0.600      B
     6     11         0.545      A
     7     12         0.583      A
     8     13         0.615      B
     8     14         0.571      A
     9     15         0.600      B
     9     16         0.563      A
    10     17         0.588      A
    11     18         0.611      B
    11     19         0.579      A
    12     20         0.600

。等等。