题解 | #字符串分隔#

字符串分隔

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

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 11:35
程序员小白条:话太多,没实力和学历,差不多回答回答就行了,身份地位不一样
点赞 评论 收藏
分享
06-15 18:44
黄淮学院 Java
Lynn012:如果是居民楼还是算了吧,看着有点野呢
点赞 评论 收藏
分享
05-19 15:21
已编辑
门头沟学院 Java
白火同学:你才沟通了200,说实话,北上广深杭这里面你连一座城市的互联网公司都没投满呢,更别说还有各种准一线二线城市了。等你沟通突破了三位数,还没结果再考虑转行的事吧。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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