题解 | #字符串分隔#

字符串分隔

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;
// }
全部评论

相关推荐

码农索隆:竞争压力小,就你一个不用卷
点赞 评论 收藏
分享
07-20 21:57
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务