题解 | #字符串分隔#
字符串分隔
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; }