题解 | #字符串分隔#
字符串分隔
http://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
思路:
-
循环读取字符串
-
对每个字符串进行处理即可
2.1 处理时,只要判断一下长度是不是8的倍数,若不是,直接添加零即可
2.2 进行输出
#include <string>
using namespace std;
int main()
{
string s1;
while(cin >> s1)
{
//对s1进行处理即可
//方法应该有多种吧,但是其实本质都差不多,直接进行遍历,肯定要比去sub更好,时间更好
if(0 != s1.length() % 8)
{
s1.insert(s1.end(), 8 - s1.length() % 8, '0');
}
for(int i = 1; i <= s1.length(); ++i)
{
if(0 == i % 8)
{
cout << s1[i - 1];
cout << endl;
continue;
}
cout << s1[i - 1] ;
}
}
return 0;
}