题解 | #字符串分隔#

字符串分隔

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

思路很简单,在string之后补0 以达到8的倍数。然后用substr截取输出

#include<bits/stdc++.h> using namespace std; int main(){

string s;
while (getline(cin,s)){
    int length = s.size();
    int cnt = length %8;
    int num = length /8;
    if(cnt != 0){
        num ++;
    }//加行
    if((cnt>0)&&(cnt<8)){
        for(int i = 0; i < 8-cnt;i++){
            s += '0';
        }
    }//补零

    for(int i = 0;i < num;i++){
        string temp = s.substr(8*i,8);
       cout<<temp<<endl;
    }
}
return 0;

}

全部评论

相关推荐

快点约我面试吧
投递百度等公司10个岗位
点赞 评论 收藏
分享
06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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