题解 | #字符串分隔#
字符串分隔
http://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
总觉得这种while循环处理输入的方法不是很好,自己实际测试的时候不好退出while循环,而且是输入一行输出一行,和题目总觉得不是很对应,但是暂时也没有想起来其他的方法,只能先这样了。
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
void solstring(int n,string s)
{
if (n > 8)
{
string res;
res = res.append(s, 0, 8);
cout << res << endl;
int n2 = n - 8;
if (n2 > 8)
{
solstring(n2, s.substr(8, 8 + n2));
}
else
{
string res2;
res2 = res2.append(s, 8, n);
int n3 = 8 - n2;
for (int i = 0; i < n3; i++)
{
res2 = res2.append("0");
}
cout << res2 << endl;
}
}
if (n == 8)
{
cout << s << endl;
}
if (n < 8)
{
for (int i = n; i < 8; i++)
{
s = s.append("0");
}
cout << s << endl;
}
}
int main(void) {
string s;
string s2;
vector<string> str;
while (getline(cin, s2))
{
//str.push_back(s2);
solstring(s2.size(), s2);
}
//for (int i = 0; i < s2.size(); i++)
//{
// string temp = str[i];
//}
system("pause");
return 0;
} 
查看16道真题和解析