平滑的加权轮询算法
代码:
#include <cstdio>
int main()
{
// 初始权重
int a_ini[3] = {4, 2, 1};
int a[3] = {4,2,1};
// 动态变化权重
int a1[3] = {1,1,1};
// 总权重
int nAllWeight = a[1] + a[2] + a[0];
for (int i = 0; i < 10; i++)
{
printf("%d %d %d\t", a[0], a[1], a[2]);
int index = 0;
if (a[index] + a1[index] < a[1] + a1[1])
{
index = 1;
}
if (a[index] + a1[index] < a[2] + a1[2])
{
index = 2;
}
a[index] -= nAllWeight;
// 变化后的权重
printf("%d %d %d\n", a[0], a[1], a[2]);
// 下次开始权重
for (size_t i = 0; i < 3; i++)
{
a[i] += a_ini[i];
}
}
while (true)
{
getchar();
}
return 0;
}
执行结果:
原理文章:
https://blog.csdn.net/larry_zeng1/article/details/80407745
https://www.jianshu.com/p/836193df61db