zoukankan      html  css  js  c++  java
  • HJ4 字符串分隔

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/ )
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址: https://www.cnblogs.com/strengthen/p/15553162.html
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    热烈欢迎,请直接点击!!!

    进入博主App Store主页,下载使用各个作品!!!

    注:博主将坚持每月上线一个新app!!!

    描述

    •连续输入字符串,请按长度为8拆分每个输入字符串并进行输出;
    •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。

    输入描述:

    连续输入字符串(输入多次,每个字符串长度小于等于100)

    输出描述:

    依次输出所有分割后的长度为8的新字符串

    示例1

    输入:
    abc
    123456789
    输出:
    abc00000
    12345678
    90000000
    while let str = readLine() {
        
        if str.isEmpty {
            continue
        }
        var tempstr = str
        if str.count % 8 != 0 {
            tempstr += "00000000"
        }
        while  tempstr.count >= 8 {
            print(tempstr.prefix(8))
            tempstr = String(tempstr.dropFirst(8))
        }
    }
    let n = 8
    while let str = readLine() {
        var tempStr = str
        let count = tempStr.count
        if count > n {
            for (index, _) in tempStr.enumerated() {
                if (index + 1) % n == 0 && index != 0 {
                    print(tempStr.prefix(n))
                    tempStr.removeFirst(8)
                }
            }
            let suffixCount = count % n
            if suffixCount != 0 {
                let suffixStr = tempStr.suffix(suffixCount)
                print(suffixStr + String.init(repeating: "0", count: n - suffixCount))
            }
        } else if count == n {
            print(str)
        } else {
            print(str + String.init(repeating: "0", count: n - str.count))
        }
    }
  • 相关阅读:
    LeetCode 169
    LeetCode 152
    LeetCode 238
    LeetCode 42
    LeetCode 11
    GDB基本调试
    小咪买东西(最大化平均值)
    codeforces 903D
    hdu 5883
    hdu 5874
  • 原文地址:https://www.cnblogs.com/strengthen/p/15553162.html
Copyright © 2011-2022 走看看