题解 | #字符串分隔#

字符串分隔

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

主要在于判断字符串长度,小于等于8只有1个字符串输出;大于8时除8不开添加一串len/8+1,整除8直接len/8

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    string s;
    getline(cin, s);

    int len = size(s);

    // vector<string> res;
    
    int nums = len <= 8? 1: len%8==0? len/8: len/8+1;
    
    for(int i = 0; i < nums; i++){
        if(i*8+8>len){
            string sub = s.substr(i*8);
            int len_sub = size(sub);
            sub.insert(len_sub, 8-len_sub, '0'); //结尾处添加0
            cout << sub <<endl;
            // res.push_back(sub);
        }else {
            string sub = s.substr(i*8,8);
            cout << sub << endl;
            // res.push_back(sub);
        }
    }
    
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

牛客38347925...:9,2学生暑期实习失利开始投小厂,给这群人整自信了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 11:15
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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