华为oj之字符串分割
来源:互联网 发布:网络推广属于什么岗位 编辑:程序博客网 时间:2024/05/01 07:03
题目: 字符串分隔
- 热度指数:6139 时间限制:1秒 空间限制:32768K
- 本题知识点: 字符串
题目描述
•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
输入描述:
连续输入字符串(输入2次,每个字符串长度小于100)
输出描述:
输出到长度为8的新字符串数组
输入例子:
abc123456789
输出例子:
abc000001234567890000000
在线提交网址:
http://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7?tpId=37&tqId=21227&rp=&ru=/ta/huawei&qru=/ta/huawei/question-ranking
分析:
根据字符串长度, 分length>8, <8, =8 三种情况即可, 另外还有一种递归的方法可以解决.
已AC代码:
#include<cstdio>#include<iostream>#include<string>#include<algorithm>using namespace std;int main(){ string str; while(getline(cin, str)) { if(str.empty()) return 0; if(str.length() == 8) cout<<str<<endl; else if(str.length() < 8 && str.length()>0) { cout<<str; for(int i = 0; i <= 8-str.length()-1; i++) cout<<'0'; cout<<endl; } else if(str.length() > 8) { int lastlen=str.length() % 8; int frontlen = str.length()-lastlen; for(int i = 0; i <= frontlen - 1; i++) { if(i==0 || i%8 != 7) cout<<str[i]; if(i%8 == 7) { cout<<str[i]<<endl; continue; } } for(int j = 0; j <= lastlen-1; j++) cout<<str[frontlen+j]; if(lastlen>0) { for(int i = 0; i <= 8-lastlen-1; i++) cout<<'0'; cout<<endl; } } } return 0;}
递归解法:
#include <iostream>#include <string>using namespace std;void process(string str) { if (str == "") return; if (str.size() <= 8) { str.append(8 - str.size(), '0'); cout << str << endl; return; } cout << str.substr(0, 8) << endl; process(str.substr(8, str.size()));}int main() { string str1, str2; cin >> str1 >> str2; process(str1); process(str2); return 0;}
1 0
- 华为oj之字符串分割
- 【华为oj】字符串分割
- 华为OJ-字符串分割
- 华为oj 字符串分割
- 华为OJ 字符串分割
- 【华为 OJ】 字符串分割
- 【华为 OJ 】字符串分割
- 华为OJ:字符串分割
- 华为oj 字符串分割
- 【华为OJ】【063-字符串分割】
- 华为OJ——字符串分割
- OJ 系列之字符串分割
- OJ系列之---字符串分割
- 华为oj之字符串反转
- 华为OJ之: 计算字符串的距离
- 华为OJ_1960_字符串分割
- 华为基础训练---字符串分割
- OJ--------字符串分割
- 多线程学习-ListenableFuture使用介绍以及示例
- 构建自己的 LINUX 系统(二)
- 算法-第四版-练习1.3.5解答
- nginx upstream的五种分配方式
- 变强
- 华为oj之字符串分割
- TreeMap
- 剑指offer--面试题50:树中两个结点的最低公共祖先
- 【python学习笔记】python使用sqlalchemy连接mysql数据库
- UNITY多语言化的工具I2 Localization使用资料分享(一)
- AS3与JAVA socket
- Android ORM框架之GreenDAO(一) 初识
- 原生JS实现的简单“瀑布流”布局
- Ball(AOJ 0033)二进制枚举