如何使用 C++ 实现分配策略
how do you implement allocating strategy with c++
我有两个工人。如果我配置了一个策略,即 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
的作业的countA
,count
作业总数(为了简化计算),并将它们初始化为零。
然后,当作业进入时,按以下方式分配:
- 如果
count
等于零,则将其分配给A
并递增countA
和count
。 - 否则,如果
countA / count
小于0.6
,则将其分配给A
并递增countA
和count
。 - 否则将其分配给
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
。等等。
相关文章:
- 将数组的地址分配给变量并删除
- vector.resize()中的分配错误
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- Win32编译器选项和内存分配
- 函数中堆分配的效果与缺少堆分配的情况
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 获取字符串的长度并将其分配给数组
- 将地址分配给本地指针后,公共对象的变量将消失
- C++17中的并行执行策略
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 我在二维向量中是否正确分配了内存
- 正在尝试重载二进制搜索树分配运算符
- 更改矢量内存分配策略
- STD :: vector和std ::字符串重新分配策略
- 矢量内存分配策略
- 如何使用 C++ 实现分配策略
- 排队对象的分配策略
- std::字符串分配策略