题解 | #字符串分隔#
字符串分隔
https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
主要在于判断字符串长度,小于等于8只有1个字符串输出;大于8时除8不开添加一串len/8+1,整除8直接len/8
#include <iostream> #include <string> #include <vector> using namespace std; int main() { string s; getline(cin, s); int len = size(s); // vector<string> res; int nums = len <= 8? 1: len%8==0? len/8: len/8+1; for(int i = 0; i < nums; i++){ if(i*8+8>len){ string sub = s.substr(i*8); int len_sub = size(sub); sub.insert(len_sub, 8-len_sub, '0'); //结尾处添加0 cout << sub <<endl; // res.push_back(sub); }else { string sub = s.substr(i*8,8); cout << sub << endl; // res.push_back(sub); } } } // 64 位输出请用 printf("%lld")