题解 | #字符串分隔#

字符串分隔

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")

全部评论

相关推荐

专业嗎喽:个人信息名字太大,合到电话邮箱那一栏就行,有党员写过党,剩下其他全删,站空太大了 把实习经历丰富,放最前面,然后是个人评价,技能之类的,然后是学校信息。项目经历最后面,可以就选一个自己擅长的。 现在是学校不是92就扣分的,没必要放前面。 然后现在看重实习经历>竞赛经历(校园经历)>课程项目经历
点赞 评论 收藏
分享
10-21 17:42
酷酷的喜马拉雅山:你为什么发我的offer列表?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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