OD机试题目10

题目:

数组切割加拼接

解答:

这道题不难,但是如何优雅的拼是个问题,解答中的做法实在是无奈之举


int main() 
{
    int aim;
    int num;
    cin >> aim;
    cin >> num;
    vector<vector<int>> res;
    int max = 0;
    while (num--)
    {
        string str;
        cin >> str;
        vector<int> tmp = str2vint(str, ',');
        max = tmp.size() > max ? tmp.size() : max;
        res.push_back(tmp);
    }

    vector<int> rest;
    int usednum = 0;
    int cnt = 0;
    while (cnt * aim < max)
    {
        for (int i = 0; i < res.size(); i++)
        {
            for (int j = aim*cnt; j < (aim * cnt+ aim) &&j < res[i].size(); j++)
            {
                rest.push_back(res[i][j]);
            }
        }
        cnt++;
    }

    int comm = 0;
    for (auto it : rest)
    {
        if (comm == 1)
        {
            cout << ',';
        }
        cout << it;
        comm = 1;
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值