c++ list分配任务


#include <iostream>
#include<stdio.h>
#include <list>

using namespace std;

list<string> subList(list<string> list1, int s, int e);

int main() {
    list<string> source;
    source.emplace_back("0");
    source.emplace_back("1");
    source.emplace_back("1");
    source.emplace_back("0");

    int n = 2;
    list<list<string>> result;
    int remainder = source.size() % n;  //先计算出余数
    int number = source.size() / n;  //然后是商
    int offset = 0;//偏移量(用以标识加的余数)
    for (int i = 0; i < n; i++) {
        list<string> value;
        if (remainder > 0) {
            int s = i * number + offset;
            int e = (i + 1) * number + offset + 1;
            value = subList(source, s, e);
            remainder--;
            offset++;
        } else {
            int s = i * number + offset;
            int e = (i + 1) * number + offset;
            value = subList(source, s, e);
        }
        result.emplace_back(value);
    }
    return 0;
}


list<string> subList(list<string> list1, int start, int end) {
    list<string> source;
    auto it = list1.begin();
    bool is = false;
    for (int i = 0; i < list1.size(); ++i) {
        string name = *it;
        if (i == start) {
            is = true;
        }
        if (i == end) {
            break;
        }
        if (is) {
            source.emplace_back(name);
        }
        it++;
    }
    return source;
}

找了这么多结果没一个c++分配任务的算法,还得自己写一个,分享给大家复制粘贴

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值