首页 > 其他 > 详细

80-20法则

时间:2020-10-08 18:22:07      阅读:27      评论:0      收藏:0      [点我收藏+]
#include <vector>
#include <stdlib.h> 
#include <time.h>       /* time */

using namespace std;

class Person
{
public:
    bool consume_money() {
        if(money >= 10) {
            money -= 10;
            return true;
        } else {
            return false;
        }
    }
    void earn_money() {
        money += 10;
    }
    void show_money() {
        printf("%d\t", money);
    }

private:
    uint32_t money = 100;
};

int main()
{
    vector<Person> society(10);
    srand(time(NULL));
    auto consumption_cycle = [&society] {
        for(auto &p : society) {
            bool re = p.consume_money();
            if (!re)continue;
            uint8_t producer_id = (uint8_t)(rand() % 10);
            society[producer_id].earn_money();
        }            
    };

    for (int i = 0; i < 100000000; i++) {
        consumption_cycle(); //100000000 cycles later
    }

    for (auto &p : society) {
        p.show_money();
    }
    getchar();
    return 0;
}

结果:

技术分享图片

80-20法则

原文:https://www.cnblogs.com/awiki/p/13781066.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!