题解 | #字符串分隔#(python3) 迭代/递归

字符串分隔

http://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7

# 递归解法:每次操作O(1),递归n次,所以时间O(n)。空间O(1)
def reduce(str1):
    len1=len(str1)
    if len1==8: print(str1)
    elif len1<8: 
        num=8-len1
        print("{}{}".format(str1,"0"*num))
    elif len1>8:
        print(str1[0:8]) #注意点:分片是左闭右开的
        str1=str1[8:]
        reduce(str1)

str1=input()
reduce(str1)
# 迭代解法:一个循环所以时间O(n)。空间O(1)
while True:
    try:
        str1=input()
        right=len(str1) 
        step=8
        for i in range(0,right,step):
            #format左对齐,不足补0
            print("{0:0<8}".format(str1[i:i+step]))
            
    except:
        break
华为机试(python3) 文章被收录于专栏

少壮不努力,老大勤刷题

全部评论

相关推荐

不愿透露姓名的神秘牛友
05-25 18:29
点赞 评论 收藏
分享
07-10 14:08
已编辑
江西农业大学 Java
念旧select:做完把项目放到自己硬盘里给他看,看完拷走
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务