•输入一个字符串,请按长度为8拆分每个输入字符串并进行输出;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
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); } } } }
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)); } }
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) } }
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); } } } }
#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; }
#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处 } }
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(); } } } }
#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; }
#方法一:先划分再补齐0
def apd_0(s):#补0
a = list(s)
for i in range(8-len(s)):
a.append('0')
res= ''
for item in a:
res+=item
return res
def func():
string = input()
n = len(string)
if n < 8:
res1 = apd_0(string)
print(res1)
else:
lst = []
for i in range(0,n,8):
lst.append(string[i:i+8])
res2 = apd_0(lst[-1])#结果列表最后一个字符串不满8位
lst[-1]=res2
for item in lst:
print(item)
if __name__ == '__main__':
while True:
try:
func()
except:
break
#方法二:先补齐0再划分 def func(): s = input() if len(s)%8 != 0: for i in range(8-len(s)%8): s += '0' for i in range(0,len(s),8): print(s[i:i+8]) if __name__ == '__main__': while True: try: func() except: break