题解 | #字符串分隔#

字符串分隔

http://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7

根据题目的要求,需要定义一个接口,传入字符串,输出字符串的数组。故定义void func(string& src, vector<string>& dst);若长度小于等于8则补0即可,若长度大于8则长次substr子串,子串长度为8,偏移位置每次递增8,若substr的子串长度小于8则表示已经结束,并且需要补0</string>

#include <iostream>
#include <vector>
#include <string>

using namespace std;

void func(string& src, vector<string>& dst)
{
    if(src.length() <= 8)
    {
        src.append(8-src.length(),'0');
        dst.push_back(src);
    }
    else
    {
        int offset = 0;
        while(offset <= src.length()-1)
        {
            string tmp = src.substr(offset,8);
            if(tmp.length() < 8)
                tmp.append(8-tmp.length(),'0');
            dst.push_back(tmp);
            offset += 8;
        }
    }
}

int main()
{
    string str;
    while(cin >> str)
    {
        vector<string> output;
        func(str, output);

        for(auto& o: output)
        {
            cout << o << endl;
        }
    }
    return 0;
}
全部评论

相关推荐

01-16 18:34
四川大学 Java
欢迎加入AI:没有啥稳定不稳定,一切都源于业务快速发展还是收缩。我当年一开始去的央企,业务不赚钱,也贼卷,慢慢就开始优化了。。。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务