题解 | #字符串分隔#
字符串分隔
http://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
# include<stdio.h>
# include<unordered_map>
# include<vector>
# include<string.h>
using namespace std;
// 方法一: 聪明的方法,调用函数更快乐~
int main(){
string str;
while(getline(cin, str)){
while(str.size() > 8){
cout << str.substr(0, 8) << endl; // substr(起始索引, 子串长度)
str = str.substr(8);
}
cout << str.append(8 - str.size(), '0') << endl;
}
return 0;
}
// 方法二: 笨方法,全手写,不调用substr()函数
// int main(){
// string s;
// cin >> s;
// int len = s.length();
// if(len == 0){
// cout << s << endl;
// return 0;
// }
// int i = 1;
// while(len / 8 > 0){
// for(int j=(i-1)*8 + 0; j<i*8;j++){
// cout << s[j];
// }
// cout << endl;
// len -= 8;
// i++;
// }
// int len0 = 8 - len;
// if(len0 != 8){
// for(int k = (i-1) * 8 +0; k < (i-1) * 8 + len; k++){
// cout << s[k];
// }
// for(int m = 0; m < len0; m++){
// cout << '0';
// }
// }
// cout << endl;
// return 0;
// }