牛客网 - 在线编程 - 华为机试 - 字符串分割

题目描述
连续输入字符串(输出次数为N,字符串长度小于100),请按长度为8拆分每个字符串后输出到新的字符串数组,

长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。

首先输入一个整数,为要输入的字符串个数。

例如:

输入:2

  abc

  12345789

输出:abc00000

  12345678

  90000000

输入描述:
首先输入数字n,表示要输入多少个字符串。连续输入字符串(输出次数为N,字符串长度小于100)。

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

示例1
输入

2
abc
123456789

输出

abc00000
12345678
90000000

c++:

注意输入形式

#include<iostream>
#include<string>
using namespace std;

void newoutput(string);
int main()
{
    string s;
    int n;
    while(cin >> n)
    {
        cin.get(); //接收一个字符(这里是cin后的换行符)
        while(n--)
        {
            getline(cin, s);
            newoutput(s);
        }
    }
    return 0;
}
void newoutput(string s)
{
    int n = s.size();
    if (n <= 8)
    {
        cout << s.append(8 - n, '0') << endl;
    }
    else
    {
        cout << s.substr(0,8) << endl;
        s = s.substr(8);
        newoutput(s);
    }
}

python:

def newoutput(s):
    if len(s) <= 8:
        print("{:0<8s}".format(s))
    else:
        print(s[0:8])
        s = s[8:]
        newoutput(s)

while True:
    try:
        n = int(input())
        for i in range(n):
            newoutput(input())
    except:
        break

猜你喜欢

转载自blog.csdn.net/qq_39735236/article/details/82081082