题解 | #字符串分隔#

字符串分隔

http://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7

利用数学表达式求余,补充末尾0,然后substr分割输出。

#include <bits/stdc++.h>
using namespace std;

int main(){
    string str;
    getline(cin, str);
    int index = str.length();
    int addZero = 8 - index % 8;
    while (addZero > 0 && addZero < 8)
    {
        str += '0';
        addZero--;
    }

    while (str.length() > 0)
    {
        cout << str.substr(0, 8) << endl;
        str = str.substr(8);
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务