•输入一个字符串,请按长度为8拆分每个输入字符串并进行输出;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
输入描述:
连续输入字符串(每个字符串长度小于等于100)
输出描述:
依次输出所有分割后的长度为8的新字符串
解法1:
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
while(cin >> s){
int len = s.size();
while(len >= 8){
for(int i = 0; i < 8; i++){
cout << s[i];
}
cout << endl;
s.erase(0, 8);
len -= 8;
}
if(len != 0){
for(int i = 0; i< 8-len; i++)
s += "0";
cout << s << endl;
}
}
return 0;
}
解法2
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
while(cin >> s){
int len = s.size();
int rm = len % 8;
if(rm > 0){
for(int i = 0; i < 8 - rm; i++){
s += "0";
}
}
int Nlen = s.size();
int j = 0;
for(int i = 8; i<=Nlen; i += 8){
cout << s.substr(j,8) << endl;
j = i;
}
}
return 0;
}
4211

被折叠的 条评论
为什么被折叠?



