【问题标题】:how do you implement allocating strategy with c++ [closed]你如何用 C++ 实现分配策略 [关闭]
【发布时间】:2015-01-03 03:31:59
【问题描述】:

我有两个工人。如果我配置了一个策略,即 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;    
}

【问题讨论】:

    标签: c++ stl


    【解决方案1】:

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

    然后,当有工作进来时,按以下方式分配:

    • 如果count 等于0,则将其分配给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
    

    ...等等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-24
      • 2010-09-07
      • 1970-01-01
      • 2010-11-27
      • 2021-07-02
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      相关资源
      最近更新 更多