题解 | #字符串分隔#
字符串分隔
https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
#include <iostream> #include <string> using namespace std; int main() { string s; getline(cin, s); if (s.size() == 0) return 0; //空字符串 int n = s.size(); //字符的长度 while (n >0) { //先判断长度是否为8的倍数 if (n >= 8) { cout << s.substr(0, 8) << endl; //输出长度为8的字符串 //要将0到第8个元素删掉 s.erase(0, 8); n = n - 8; } else { n = 8 - n; //补零的个数 for (; n > 0; n--) { s += '0'; } cout << s << endl; n = -1; //结束循环 } } return 0; }