题解 | 字符串分隔
字符串分隔
https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7
#include <iostream> #include<vector> #include<cmath> using namespace std; int main() { string s; cin>>s; //计算所需行数 int n = int (ceil(double(s.length())/double(8)));//ceil利用double数据计算,用int型会导致两个计算的数据直接截取整数部分,导致结果出现偏差 //计算所需行数后再进行容器的建立 vector<string> line(n); size_t pos = 0; //处理前面n-1段 for(int i = 0; i < n - 1; i++){ line[i] = s.substr(pos,8) ; pos+=8; } line[n-1] = s.substr(pos); //为第n段补‘0’ while(line[n-1].length() < 8){ line[n-1] += '0'; } //遍历输出 for( int j = 0; j < n ; j++ ){ cout<<line[j]<<endl; } } // 64 位输出请用 printf("%lld")