首页 > 试题广场 >

字符串分隔

[编程题]字符串分隔
  • 热度指数:1133612 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

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

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

输入描述:

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



输出描述:

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

示例1

输入

abc

输出

abc00000
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){         
        	String s = new String(sc.nextLine());
        	if(s.length()%8 !=0 )
        		s = s + "00000000";
        	
        	while(s.length()>=8){
        		System.out.println(s.substring(0, 8));
        		s = s.substring(8);
        	}
        }
    }
}

发表于 2016-07-07 23:26:13 回复(165)
有没有 增加题目示例的可能???
或者题目说明说清楚一点。。。。
简简单单的题,最大的难点在猜测题目到底要干什么,到底会怎样的形式输入(这个题还好,输入可以tackle,说不清楚也能解决掉),应该怎么输出呢?是分割后的字符串直接接续输出?还是分割后的字符串空一格输出?还是换一行输出????????
只能靠提交测试题目的用意
浪费时间
发表于 2022-06-06 22:41:50 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int start = 0;
        int len = s.length();
        while (len > 8) {
            System.out.println(s.substring(start, start + 8));
            start += 8;
            len -= 8;
        }
        String half = s.substring(start  , len+start);
        for (int i=0;i<8;i++){
            half+="0";
        }
        System.out.println(half.substring(0,8));
    }
}

发表于 2022-06-04 03:47:35 回复(0)
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	result := make([]string, 0)
	for scanner.Scan() {
		input := scanner.Text()
		input += "0000000"
		for len(input) >= 8 {
			result = append(result, input[0:8])
			input = input[8:]
		}
	}
	for _, item := range result {
		fmt.Println(item)
	}
}


发表于 2022-05-22 16:37:25 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str = new String(sc.nextLine());
            if (str.length() % 8 != 0) {
                str = str + "00000000";
            }
            while (str.length() >= 8) {
                System.out.println(str.substring(0, 8));
                str = str.substring(8);
            }
        }
    }
}

发表于 2022-05-16 14:51:51 回复(0)
a = input()

n = 0
for i in a:
    print(a[n],end="")
    n += 1
    if n %8 == 0:
        print()
    elif n %8 !=0 and n ==len(a):
        for j in range(8 - n%8):
            print(0,end="")
发表于 2022-04-15 18:35:03 回复(0)
s=input()
while len(s)>8:
    print(s[:8])
    s=s[8:]
print(s.ljust(8,'0'))
发表于 2022-04-14 10:56:34 回复(0)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string line;
    cin>>line;
    while(line.size()%8>0)
    {
        line+='0';
    }
    int index=0;
    while(index<line.size())
    {
        cout<<line[index++];
        if(index%8==0)cout<<endl;
    }
}

发表于 2022-04-08 23:03:32 回复(0)
#include<stdio.h>
#include<string.h>
int main(void)
{
    char data[101];//定义字符数组
    while(gets(data))//获取字符串
    {
        int m=strlen(data);//计算字符串的长度
        int count=0;
        for(int i=0;i<m;i++)
        {
            printf("%c",data[i]);//输出字符串中的每一个字符
            count++; //用于控制换行符个数
            if(count%8==0)
            {
                printf("\n");
            }
        }
       int n=m/8,k=m-n*8,l=8-k;//计算所需要的字符零的个数
        while(l--&&m%8!=0) //要保证原本字符串的长度不是八的倍数
        {
            printf("0");
        }
        printf("\n");
    }
    return 0;
}

发表于 2022-04-02 16:01:41 回复(0)
#include <bits/stdc++.h>
using namespace std;
int main()
{
    string n;
    while (cin>>n)
    {
        int m=n.size();
        if(m/8)//大于8位的情况
      {
        if(m%8)//大于8位且不是8的整倍数
        {for (int i=0;i<m/8;i++)
        {
            cout<<n.substr(8*i,8)<<endl;
        } 
         string s1 (8-m%8,'0');
        cout<<n.substr(8*(m/8),m-8*(m/8))+s1<<endl;}
        else   //大于8位且是8的整倍数
        {
            for (int i=0;i<m/8;i++)
            {
                cout<<n.substr(8*i,8)<<endl;
            }
        }
      }
        else//小于8位的情况
        {string s2 (8-m,'0');
            cout<<n+s2<<endl;
        }
    }  
   
    return 0;
}

发表于 2022-03-30 14:50:04 回复(0)
分两种情况判断,递归
空间复杂度1,没有重复
时间复杂度

def split(string):
    if len(string) <= 8:
        print(string + "0" * (8-len(string)))
    else:
        len(string)> 8 
        print(string[:8])
        split(string[8:])
while True:
    try:
        a=input()
        split(a)
    except:
        break


发表于 2022-02-24 16:02:47 回复(0)
愚之见
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.nextLine();
            if(str.length()<8){
                tian(str);
            }else{
                shu(str);
            }
        }
        

    }
    static void tian(String str){//处理小于8的字符串
       int count = 8-str.length();
       while(count>0){
            str+="0";
            count--;
        }
       System.out.println(str);
    }
    
     static void shu(String str){//处理大于等于8的字符串
        int i = str.length()%8;
        for(int j=7 ; j<str.length() ; j+=8 ){
            System.out.println(str.substring(j-7,j+1));
        }
        if(i!=0)//处理不是8的倍数的字符串
            tian(str.substring(str.length()-i));
      }
    
}


发表于 2022-02-23 16:46:12 回复(0)
while True:
    try:
        a = input()
        if len(a) % 8 != 0:
            a = str(a) + "0" * (8 - len(a) % 8)
        for i in range(int(len(a) / 8)):
            print(a[i*8:(i+1)*8])
    except:
        break
发表于 2022-02-04 14:23:06 回复(0)
方法一:#include<iostream>
#include<string>
using namespace std;
int main(void)
{
    string s;
    while(getline(cin,s))
    {
        int add=s.size()%8;
        if(add>0)
        {
            for(int x=add;x<8;x++)s=s+"0";//先补全后切分
        }        
        int npos=0;
        while(npos<s.size())
        {
            string str=s.substr(npos,8);
            cout<<str<<endl;
            npos+=8;           
        }
    } 
    return 0;
}
方法二:
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
    string s;
    while(getline(cin,s))
    {
        int tem=0;
        for(int i=0;i<s.size();i++)//先输出后补全
        {
            cout<<s[i];
            tem++;
            if(tem%8==0) cout<<endl;           
        }
        int add=s.size()%8; //以填补字符串的方式进行补充输出
        if(add==0)continue;
        string str="00000000";
        string str1=str.substr(add);
        cout<<str1<<endl;
    } 
    return 0;
}

发表于 2022-01-14 22:06:55 回复(0)
取余、换行
#include<iostream>
#include<string>
using namespace std;

int main(){
    string s;
    while(cin>>s){
        int i;
        for(i=0;i<s.length();i++){    // 正常输出字符串,每8个打印一次换行
            cout<<s[i];
            if(i%8==7 &&i!=0) //I处
                cout<<endl;
        }
        bool need=false;   // 注意需要特别处理,当I处恰好末尾结束(输出一次换行)时,需要让II处不再换行
        while(i%8 !=0){  // 最后处理一下不足8个时,补充‘0’的个数
            need = true;
            cout<<'0';
            i++;
        }
        if(i%8!=0 || need) cout<<endl; // II处
    }
}

发表于 2022-01-11 10:47:17 回复(0)
while True:
    try:
        fir=input()
        while 8<len(fir)<=100:
            print(fir[:8])
            fir=fir[8:]
        if len(fir)>100:
            print("请重新输入小于等于100的字符串:")
        else:
            print(fir+"0"*(8-len(fir)))
    except:
        break
发表于 2021-12-14 14:23:14 回复(0)
import sys 
for line in sys.stdin:
    line = line.strip()
    if len(line)%8 != 0:
        line = line + "0"*(8-len(line)%8)
    for j in range(int(len(line)/8)):
        print(line[j*8:(j+1)*8])




发表于 2021-10-26 19:53:20 回复(2)
思路:
    1、先根据输入字符串的长度,判断长度是否大于8,或者刚好等于8的整数倍,亦或者不足8;
    2、若长度大于等于8,则输出可以拼凑成长度为8的字符串;
    3、用输入字符串的长度对8取余,若余数不为0,则输出该字符串最后几个字符,并在最后补上相应数目的0。
public class Main {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        while(sc.hasNext()) {
            
            String s = sc.nextLine();
            
            int len = s.length();
            
            int n = len / 8;
            int m = len % 8;
            
            //将能组成长度为8的字符串片段输入
            for(int i = 1; i <= n; i++) {
                for(int j = (i-1)*8; j < i*8; j++) {
                    System.out.print(s.charAt(j));
                }
                System.out.println();
            }
            
            if(m != 0){
                //将长度小于8的字符串片段输出,并在后面补上若干0
                for(int i = n*8; i < 8*n+m; i++) {
                    System.out.print(s.charAt(i));
                }
            
                for(int j = 0; j < 8-m; j++) {
                    System.out.print(0);
                }
                System.out.println();
            }
        }
        
    }
}




发表于 2021-10-26 13:54:26 回复(0)
const test = function (str) {
  let array = [];
  let len = str.length;
  /// 取模补0
  let supplyZeroCount = 8 - len % 8;
  let newStr = str
  while(supplyZeroCount > 0){
      newStr = newStr + '0'
      supplyZeroCount --
  }
  /// 一定是8的倍数
  let max = newStr.length / 8
  let count = 0;
  while(count <= max){
      let s = newStr.substring(count * 8, (count + 1) * 8);
      console.log(s)
      count ++;
  }
}
发表于 2021-10-15 20:25:58 回复(0)
用指针指向字符串,遍历即可
#include <iostream>
#include <string>

using namespace std;

int main(){
    string str;
    
    while(getline(cin, str)) {
        int len = str.size();

        //cout << str;
        int index = 0; // 用指针搜索
        while (index < len) {
            char res;
            for (int j = 0; j < 8; j++) {
                if (index < len) {
                    res = str[index++];
                } else {
                    res = '0'; // 添0
                }
                cout << res;
            }
            cout << endl;
        }
    }
    
    return 0;
}


发表于 2021-09-27 17:18:57 回复(0)