Day8字符串分隔

152 阅读2分钟

Day8

题源

描述

•输入一个字符串,请按长度为8拆分每个输入字符串并进行输出;

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

输入描述:

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

输出描述:

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

示例1

输入:

abc

复制

输出:

abc00000
  • 法1:暴力破解法

    • 1.字符串长度是8的偶数,那么设置步长8,然后直接遍历字符输出
    • 2.字符串长度是8的奇数,那么需要先遍历s.lenght() / 8的部分,按照上面的做法,然后根据剩下字符串余下的数字,进行增加0操作
  • 法2:直接利用库函数append进行添加零操作,注意是直接添加到字符串最后面,substr操作是截取字符串操作
  • 法3:利用cout输出流width和fill函数直接打印,这个需要参考cout输出流的具体使用

具体解法

#include <math.h>
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <stdlib.h>
#include <cstdlib>
#include <cstring>//#include "./include/list/sqlist.h"
​
using namespace std;
​
//法1:原地算法,暴力破解法,
/**
 * @description: 
 * @param {string &} s
 * @return {*}
 */
void SplitString(string & s){
    //int k = 7;//因为是按照步长为8进行分解
    //暴力解法,先判断是否是8的倍数
    if(s.length() % 8 == 0){
        //如果是8的倍数,那么就需要往直接循环遍历打印出来
        int degree2 = s.length() / 8;
        int j2 = 0;
        for(int i = 1; i <= degree2; i++){
            int k = 8;
            while(k--){
                printf("%c",s[j2++]);
            }
            printf("\n");
        }
    }else{
        //不是8的倍数,那么需要在后面打添加0
        int zeros = s.length() %  8;//剩余的余数
        //需要进行遍历的总步数
        int degree = s.length() / 8;
        int zeros_add = 8 - zeros;
        int j = 0;
        for(int i = 1; i <= degree; i++){
            //j = i * 8 - 1;//截至位置
            //int h = j - 7;
            int k = 8;//步长
            while(k--){
                printf("%c",s[j++]);
            }
            printf("\n");
        }
        //打印余下最后一组
        //从j最后位置开始,
        while(zeros--){
            printf("%c",s[j++]);
        }
        while(zeros_add--){
            std::cout << 0;
            //printf(0);
        }
        printf("\n");
    }
}
//法2:string库函数直接调用
//append直接添加零,append的应用
//substr是直接截取主串复制到子串中
/**
 * @description: 
 * @param {string &} s
 * @return {*}
 */
void SplitStringII(string & s){
    //首先将判断字符串是否满足8的偶数
    string sub;
    int len = s.length();
    if(len % 8 != 0){
        int degree = 8 - len % 8;
        s.append(degree,'0');
    }
    int new_len = s.length();
    for(int i = 0; i < new_len; i += 8){
        std::cout << s.substr(i, 8) << std::endl;
    }
}

//参考题解,
//输出流fill和width的应用
/**
 * @description: 
 * @param {string &} s
 * @return {*}
 */
void SplitStringIII(string & s){
    string sub;
    int len = s.length();
    for(int i = 0; i < len; i += 8){
        cout.width(8);
        cout.fill('0');
        cout << left << s.substr(i,8) << endl;
    }
}

​
int main(){
​
​
    // //day06
    // LinkList L;
    // int n;//输入数据数量
    // int index = 0;//输入的位置
    // ListNode* p;
    // while(scanf("%d",&n) != EOF){
    //     L = ListTailInsert(L,n);
    //     scanf("%d",&index);
    //     if(index == 0) std::cout << 0 << std::endl;
    //     else{
    //         p = FindKInListIII(L,index);
    //         if(p != NULL) printf("%d\n",p->data);
    //     }
    //     //PrintLinkedList(L);
    // }
    // int month_;
    // while(scanf("%d",&month_) != EOF){
    //     int ans = GetRabbitNums(month_);
    //     printf("%d\n",ans);
    // }
    //day8
    string s;
    getline(cin, s);
    SplitString(s);
​
    system("pause");
    return 0;
}

复杂度分析

  • 法1:时间复杂度:O(8n)既是O(n),空间复杂度O(1)

小结

第一种解法是暴力解法,时间复杂度为O(n),但同时如果步长较大的时间开销较为大,但是空间复杂度始终为O(1)