题解 | #字符串分隔#

字符串分隔

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

利用count记录已经处理的字符数,每8个输出一次,并将count置为0,记得最后检查count是否够8,不够补0
# include<iostream>
# include<string>
using namespace std;

int main()
{
    string str;
    while(getline(cin, str))
    {
        int len = str.length();
        string tmp = "";
        int count = 0;
        for(int i=0;i<len;i++)
        {
            tmp+=str[i];
            count++;
            if(count == 8)
            {
                cout<<tmp<<endl;
                tmp = "";
                count = 0;
            }
        }
        if(tmp.length()>0&& count<8)
        {
            while(count<8)
            {
                tmp+='0';
                count++;
            }
        }
        cout<<tmp<<endl;
    }
    return 0;
}


全部评论

相关推荐

05-20 13:59
门头沟学院 Java
米黑子米黑子:你这个成绩不争取下保研?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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