字符串分割
OJ 地址:字符串分割
#include <iostream>
#include <vector>
using namespace std;
void print(string str)
{
if(str.size() < 8) // 字符串长度小于8,填充0
{
int n = 8 - str.size();
for(int i = 0; i < n; i++)
str += '0';
cout << str << endl;
return;
}
else if(str.size() == 8) // 字符串长度等于8,直接输出
{
cout << str << endl;
return;
}
else //字符串大于长度大于8
{
int flag = str.size() % 8 == 0 ? 0 : 1; // 判断字符个数是否能被8整除
if(flag == 0) // 能整除
{
for(int i = 0; i < str.size(); i += 8)
{
cout << str.substr(i, 8) << endl;
}
}
else // 不能整除
{
int count = str.size() / 8;
int j = 0;
while(count --)
{
cout << str.substr(j, 8) << endl;
j += 8;
}
int n = 8 - (str.size() % 8);
for(int i = 0; i < n; i++)
str += '0';
cout << str.substr(j, 8) << endl;
}
}
}
int main()
{
vector<string> res;
string str;
cin >> str;
res.push_back(str);
cin >> str;
res.push_back(str);
for(string s : res)
{
print(s);
}
}