题解 | #字符串分隔#
字符串分隔
http://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int main()
{
string str,res;
while(cin>>str)
{
int mod=str.length()%8; //取余,余数如果不为零,在后面补上8-mod个0
int qu=str.length()/8; //作为后面的循环,本来在这里是+1了,后面发现每次8的整数倍就多一行回车,所以把+1放到有余数的里面,没有余数的只用循环qu次
auto i=0;
if(mod!=0)
{
while(mod<8)
{
str=str+"0";
mod++;
}
qu=qu+1;
}
while(qu--)
{
res=str.substr(0+i*8,8);
i++;
for(auto t:res) cout<<t;
cout<<endl;
}
}
#include<string>
#include<string.h>
using namespace std;
int main()
{
string str,res;
while(cin>>str)
{
int mod=str.length()%8; //取余,余数如果不为零,在后面补上8-mod个0
int qu=str.length()/8; //作为后面的循环,本来在这里是+1了,后面发现每次8的整数倍就多一行回车,所以把+1放到有余数的里面,没有余数的只用循环qu次
auto i=0;
if(mod!=0)
{
while(mod<8)
{
str=str+"0";
mod++;
}
qu=qu+1;
}
while(qu--)
{
res=str.substr(0+i*8,8);
i++;
for(auto t:res) cout<<t;
cout<<endl;
}
}
}
//strncpy,本来想用这个函数,但是发现C++里面string类这样写不好用,总是出错。