首页 > 编程 > C++ > 正文

[华为OJ--C++]004-字符串分隔

2019-11-08 18:45:29
字体:
来源:转载
供稿:网友

题目描述:连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。

输入描述:输入字符串的行数,连续输入字符串,每个字符串长度小于100

输出描述:输出到长度为8的新字符串数组

输入例子:

2

abc

123456789

输出例子:

abc00000

12345678

90000000

算法实现:

#include<iostream>#include<vector>#include<string>using namespace std;/************************************************  * Author: 赵志乾  * Date: 2017-2-16   * Declaration: All Rigths Reserved !!!  ***********************************************/ int main(){    int line;    cin>>line;        vector<string>ret;    for(int i=0;i<line;i++)    {		string instr;        cin>>instr;		while(instr.length()%8!=0)		{			instr+="0";		}        for(int i=0;i<instr.length()/8;i++)        {        	ret.push_back(instr.substr(i*8,8));		}	}	for(int i=0;i<ret.size();i++)		cout<<ret[i]<<endl;	return 0;}

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选